【问题标题】:CustomUser relation doesn't exist : Django migration errorCustomUser 关系不存在:Django 迁移错误
【发布时间】:2021-07-07 10:46:11
【问题描述】:

我对 Django 很陌生,在尝试创建自定义用户时遇到了问题。 我按照列出的所有步骤创建了一个,但是因为我最初是从默认用户模型开始的,所以我删除了所有迁移和我的 postgres 数据库以从头开始(如果没有,我会阅读它会导致问题)。

makemigrations 工作正常,但是当我想迁移时,我收到以下错误:

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)

我的 docker 容器还输出以下内容:

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

我到处寻找类似的问题及其解决方案,但到目前为止没有运气尝试解决此问题...

提前感谢您的帮助!!

【问题讨论】:

  • 设置中INSTALLED_APPS中没有定义自定义用户模型的app吗?
  • 在设置中...像这样,INSTALLED_APPS = [ 'cyclabApp.apps.CyclabappConfig',
  • 您是否有机会删除了应用程序的整个迁移文件夹?尝试运行python manage.py makemigrations cyclabApp
  • 嗯,我想我从来没有删除过整个文件夹。我试图以这种方式运行 makemigrations,但之后迁移仍然给我同样的错误......

标签: django django-migrations django-custom-user


【解决方案1】:

好的,我解决了。 显然我错过了我的 UserManager 的模型。在我添加之后工作。 :-)

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

【讨论】:

    猜你喜欢
    • 2017-07-01
    • 2019-02-04
    • 2017-03-25
    • 2015-04-28
    • 2016-08-15
    • 2016-02-14
    • 1970-01-01
    • 2021-07-26
    • 2018-04-07
    相关资源
    最近更新 更多