CustomUser关系不存在';不存在:Django迁移错误
我是Django的新手,在尝试创建自定义用户时遇到了一个问题。 我按照概述的所有步骤创建了一个,但因为我最初使用的是默认用户模型,所以我删除了所有迁移,并从零开始使用postgres db(如果没有,我认为这会导致问题) makemigrations工作正常,但当我想要迁移时,会出现以下错误:CustomUser关系不存在';不存在:Django迁移错误,django,django-migrations,django-custom-user,Django,Django Migrations,Django Custom User,我是Django的新手,在尝试创建自定义用户时遇到了一个问题。 我按照概述的所有步骤创建了一个,但因为我最初使用的是默认用户模型,所以我删除了所有迁移,并从零开始使用postgres db(如果没有,我认为这会导致问题) makemigrations工作正常,但当我想要迁移时,会出现以下错误: django.db.utils.ProgrammingError: relation "cyclabApp_customuser" does not exist. Applying
django.db.utils.ProgrammingError: relation "cyclabApp_customuser" does not exist.
Applying admin.0001_initial...Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "cyclabApp_customuser" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/manage.py", line 22, in <module>
main()
File "/app/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 229, in apply_migration
migration_recorded = True
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 115, in __exit__
self.execute(sql)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
我到处寻找类似的问题及其解决方案,但到目前为止还没有找到解决这个问题的方法
提前感谢您的帮助 好的,我解决了。
显然,我错过了我的UserManager的模型。在我添加之后工作。:-)
定义自定义用户模型的应用程序是否未在设置中的
已安装应用程序中?它在设置中。。。与此类似,已安装的应用程序=['cyclabApp.apps.CyclabappConfig',你有没有删除你应用程序的整个迁移文件夹?试着运行python manage.py makemigrations cyclabApp
。嗯,我想我从来没有删除过整个文件夹。我试着以那种方式运行makemigrations,但迁移之后仍然给我相同的错误。。。
2021-04-12 10:22:13.665 UTC [95] STATEMENT: ALTER TABLE "django_admin_log" ADD CONSTRAINT "django_admin_log_user_id_c564eba6_fk_cyclabApp_customuser_id" FOREIGN KEY ("user_id") REFERENCES "cyclabApp_customuser" ("id") DEFERRABLE INITIALLY DEFERRED
class UserManager(BaseUserManager):
def create_user(
self, email, first_name, last_name, password=None,
commit=True):
"""
Creates and saves a User with the given email, first name, last name
and password.
"""
if not email:
raise ValueError(_('Users must have an email address'))
if not first_name:
raise ValueError(_('Users must have a first name'))
if not last_name:
raise ValueError(_('Users must have a last name'))
user = self.model(
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name,
)
user.set_password(password)
if commit:
user.save(using=self._db)
return user