当我在新创建的Django项目中安装了“registration redux”应用程序,并运行“migrate”命令时,为什么会出现“auth_user不存在”的错误?

15
给定一个新创建的Django项目,其中安装了以下应用程序:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration',
)

第一次运行./manage.py migrate时,我遇到了以下错误:

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages, registration
  Apply all migrations: sessions, admin, auth, contenttypes
Synchronizing apps without migrations:
  Creating tables...
    Creating table registration_registrationprofile
    Running deferred SQL...
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
    cursor.execute(statement)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "auth_user" does not exist```

看起来 Django 正在尝试在用户表之前创建注册表。

如果我注释掉注册应用并运行 migrate,然后取消注释注册应用并再次运行 migrate,就不会出现这个错误。然而,那样做不是正确的方式,对吗?


Django迁移应该能够指定依赖项以确定顺序,因此registration迁移可能可以使用auth.Userdependency。由于它是一个库,我不太确定这里的解决方案是什么。也许是INSTALLED_APPS的顺序? - Yuji 'Tomita' Tomita
我已经改变了已安装应用程序的顺序,但还是不起作用!:( 顺便说一下,谢谢。 - goathi
为了“解决”这个问题,我不得不为所有应用程序创建所有迁移,然后启动迁移,它就可以工作了。https://dev59.com/P10b5IYBdhLWcg3wCtTY(Pedro的答案) - Daviddd
我遇到了同样的问题。在mysql5.6上,python manage.py migrate以正确的顺序迁移所有应用程序,首先是auth,但当我切换到mysql5.7时,出现了这个问题。 - Hussain
8个回答

25

更新Django版本后,我遇到了这个错误,通过运行以下两行代码进行修复:

python manage.py migrate auth
python manage.py migrate

我想auth模型中的auth_user表应该先运行。


如果看起来像manage.py被放在某个地方,没关系,你仍然可以使用它-#python /usr/lib/python2.7/dist-packages/graphite/manage.py migrate auth - danno
1
终于得到了我需要的答案。有无数个答案说要运行manage makemigrationsmanage migrate,但这是我第一次看到提到manage migrate auth的。这是因为我用Django 1.6创建了我的应用程序,并一路升级到2.0吗?还是大多数答案都不完整? - JFlo
更多的人需要看到这个答案。在经过大量搜索后,它解决了我的问题。谢谢。 - Ryan

4

当您按照Pedro Wagner的建议执行操作时(Django 1.8和syncdb / migrate出现auth_user错误),就可以避免该问题:

确保所有应用程序都存在初始迁移文件,方法是运行以下命令:

manage.py makemigrations my_app

我不仅希望你为依赖认证的人提供翻译,因为我认为这个问题更普遍。

这种行为的根本原因似乎是由于某种原因。

manage.py makemigrations

如果初始迁移不存在,它并不总是创建它们,与以下情况相反:

manage.py makemigrations my_app

很遗憾,我无法理解这种不对称的原因。


这非常有帮助。 - ronit

4

我认为你只是忘记了迁移你的认证模型。不过,要做到这一点,只需在终端上输入以下命令。

python manage.py migrate 

或者

python manage.py migrate auth

希望这能解决你程序中的错误。

1
我认为你需要运行以下命令:python manage.py syncdb,可能还需要设置一些依赖项。在同步数据库后可能不是必需的。
南方风格(Django < 1.7)
class Migration:

    depends_on = (
        ("accounts", "0001"),
    )

    def forwards(self):
        ....

South 2 风格(Django >= 1.7)

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [("accounts", "0001")]

0
在我的情况下,这个问题已经通过重新添加一些已经被删除的INSTALLED_APPS模块来解决。结果是,数据库中的一些表会混淆迁移计划,然后破坏test命令,因为默认数据库包含了这些之前的迁移。
在我的情况下,通过重新添加allauth和其他相关子模块进行修复。

0
一旦你为你的应用创建了迁移,它将自动添加必要的依赖项。因此,在这种情况下,只需运行./manage.py makemigrations registration
请检查registration/migrations/0001_initial.py文件,你应该会看到类似以下的内容:
dependencies = [
    migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

这意味着您需要为所有具有任何依赖关系的应用程序创建迁移。


0

我也遇到过这个问题,通过用包含 pull #25 的新注册替换旧的注册解决了它:

pip install git+https://github.com/macropin/django-registration.git@v1.2c0

-2

您尚未迁移您的模型

python manage.py makemigrations my_app_name

适用于Mac OS

python3 manage.py makemigrations my_app


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接