我正在开发一个新的django项目,但是遇到了迁移问题。
我想做一个用户模型,为了做到这一点,到目前为止我做了两件事。1。我做了AuthUser模型,并在下面的类中设置了一个 meta类。
在模型.py中
class AuthUser(AbstractUser):
user_type_id = models.PositiveIntegerField(choices=UserTypes.choices())
user_id = models.PositiveIntegerField()
class Meta(AbstractUser.Meta):
swappable = 'AUTH_USER_MODEL'
@property
def user_type(self):
return UserTypes(self.user_type_id)
def original_orm(self):
if self.user_type.value == UserTypes.raijosha.value:
return RaijoshaUsers.objects.filter(id=self.user_id).first()
elif self.user_type.value == UserTypes.shuttennsha.value:
return Users.objects.filter(id=self.user_id).first()
1.在www.example.com中settings.py,我设置了AUTH_USER_MODEL。
AUTH_USER_MODEL = '推荐验证用户'
这是错误代码。
Operations to perform:
Apply all migrations: admin, auth, contenttypes, recommend, recommend_raijousha, sessions
Traceback (most recent call last):
File "manage.py", line 25, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/migrate.py", line 164, in handle
pre_migrate_apps = pre_migrate_state.apps
File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/state.py", line 218, in apps
return StateApps(self.real_apps, self.models)
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/state.py", line 295, in __init__
raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'recommend.authuser', but app 'recommend' doesn't provide model 'authuser'.
你能帮助我们或者给予我一个建议吗?
Applying admin.0001_initial...Traceback (most recent call last):
File "manage.py", line 25, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/operations/models.py", line 97, in database_forwards
schema_editor.create_model(model)
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/schema.py", line 254, in create_model
definition, extra_params = self.column_sql(model, field)
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/schema.py", line 144, in column_sql
db_params = field.db_parameters(connection=self.connection)
File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 994, in db_parameters
return {"type": self.db_type(connection), "check": self.db_check(connection)}
File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 991, in db_type
return self.target_field.rel_db_type(connection=connection)
File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 909, in target_field
return self.foreign_related_fields[0]
File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 653, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 640, in related_fields
self._related_fields = self.resolve_related_fields()
File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 625, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'recommend.authuser' cannot be resolved
考虑到一个建议,我确实删除了迁移文件。但另一个错误发生如上所示。
8条答案
按热度按时间6yjfywim1#
如果您运行了默认
auth
应用迁移,然后在settings.py
中更改了AUTH_USER_MODEL
,则会发生这种情况。您可以尝试以下操作:cyej8jka2#
我删除所有的迁移文件和数据库,并应用它。
我就可以移民了。
hpxqektj3#
注意:我使用sqlite3作为数据库。
您好,我很容易地解决了这个问题,删除了migrations文件夹中除
__init__.py
文件之外的所有迁移文件。同时删除db.sqlite3
。现在运行以下命令:python manage.py makemigrations
,然后python manage.py migrate
。现在您必须再次创建超级用户,因此只需键入以下命令:python manage.py createsuperuser
。然后它会提示用户名,电子邮件和密码,所以输入您的凭据,所有将继续正常工作再次我希望这将是有帮助的。Click Here To View Image
vx6bjr1n4#
对我来说帮助分裂了两次迁移
1.创建新表(新表和旧表之间没有连接,并且没有
AUTH_USER_MODEL = 'recommend.authuser'
)1.将AUTH_USER_MODEL添加到settins.py和其他具有新表的连接
55ooxyrt5#
我必须参考this stack:才能解决这个问题。我只是简单地按照下面的步骤操作:
因为我没有使用sqlite,所以我在www.example.com中创建了新的数据库配置,该配置引用了我的postgressettings.py:
旧的数据库,我不打算用途:
第一个月
要开始使用的新数据库:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'leszexpert_db', 'USER': 'postgres', 'PASSWORD': '', 'HOST': '127.0.0.1', 'PORT': '5432', } }
如您所见,不同之处在于数据库名称(在引擎之后引用)!!!!!!
1.我停止了服务器,并且之前已经删除了所有现有的相应迁移。
1.我关闭了我的IDE VScode(我从经验中意识到,当某些东西没有生效时,在关闭它并重新打开它之后,它大多数时候最终生效)
1.当我重新打开代码编辑器时,我取消了对模型的注解(我之前已经注解过了),然后非常非常重要:此时请勿启动或运行您的服务器。因为现在您要对上面指定的新数据库进行操作,而您知道我们正在尝试解决的问题是,如果您在运行服务器后运行该类型模型的迁移,它将不会创建您的模型...因此,请勿运行服务器
1.而不是这样做:运行迁移,然后迁移
感谢您阅读我的回答
ubof19bj6#
我用的是MySQL。
对我来说,解决方案是删除migrations文件夹中的文件(除了__ init__.py之外的所有文件)、MySQL Workbench中的
DROP
和CREATE
数据库,然后再次运行python manage.py makemigrations
和python manage.py migrate
。因此,我不得不创建一个新的超级用户,我所有的数据都丢失了:(
zkure5ic7#
如果迁移在本地进行,而不是在服务器上进行,请在服务器上执行此操作;
ie3xauqp8#
如果您仍在开发阶段,并且可以忍受再次填写数据库数据的痛苦,那么您需要知道的就是以下内容!
you need to first delete all the files related with db(including sqlite.py if it exists), you can do this by the help of below following ways.
查找-路径"/迁移/. py"-非-名称"初始化. py"-删除
查找-路径"/迁移/. pyc"-删除
现在,要么删除数据库中的所有数据和表,要么删除数据库并创建新数据库,然后分别为每个应用程序执行迁移。
第一个月
python manage.py migrate appname
然后,如果您收到任何会话错误,如"ProgrammingError:关系"django_session"不存在
然后遵循以下步骤:
python manage.py migrate --fake sessions zero
则您的会话迁移将
python manage.py showmigrations
会话[ ] 0001_initial
then migrate with --fake-initial again
python manage.py migrate --fake-initial
现在做尝试到runserver再次和问题必须已经过去了,如果没有然后请让我知道,我会尽我最大的可能的方法来解决它。
谢谢你看这个。