Django 1.8 测试问题:ProgrammingError: 关系“auth_user"不存在

2022-01-23 00:00:00 python django django-1.8 migration

问题描述

我最近将 Django 升级到 1.8 并设置了一个新的开发数据库以重新开始.迁移和依赖关系进展顺利,避免了您遇到的常见错误并最终解决了问题.该应用程序现在在本地运行良好.

I recently upgraded Django to 1.8 and set up a new development database for a fresh start. Migrations and dependencies went well, safe the usual errors you get and you end up solving. The app is working locally now just fine.

但是,我在尝试运行测试时遇到错误:

However, I am getting an error when trying to run tests:

python manage.py test

这是我得到的错误:

django.db.utils.ProgrammingError: relation "auth_user" does not exist

不用说,django的auth模块确实是在app中安装和迁移的,所以不清楚是怎么回事.

Needless to say, Django's auth module is indeed installed and migrated in the app, so I am not sure what is going on.

这是完整的堆栈跟踪,以防您需要查看它,但它并没有说任何对我找出这个错误原因有帮助的东西:

Here is the full stacktrace in case you need to peek at it, but it does't say anything even remotely helpful to me to figure out the cause of this error:

Traceback (most recent call last):
  File "C:/Users/dabadaba/PycharmProjects/dogpatchsports_com/mysite/manage_sched_dev.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagement\__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagement\__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagementcommands	est.py", line 30, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagementase.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagementcommands	est.py", line 74, in execute
    super(Command, self).execute(*args, **options)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagementase.py", line 445, in execute
    output = self.handle(*args, **options)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagementcommands	est.py", line 90, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjango	estunner.py", line 210, in run_tests
    old_config = self.setup_databases()
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjango	estunner.py", line 166, in setup_databases
    **kwargs
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjango	estunner.py", line 370, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangodbackendsasecreation.py", line 368, in create_test_db
    test_flush=not keepdb,
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagement\__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagementase.py", line 445, in execute
    output = self.handle(*args, **options)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagementcommandsmigrate.py", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangocoremanagementcommandsmigrate.py", line 318, in sync_apps
    cursor.execute(statement)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangodbackendsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangodbutils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:UsersdabadabaEnvsdjango18libsite-packagesdjangodbackendsutils.py", line 62, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "auth_user" does not exist

我可以在 this answer 之后找到一种解决方法,它会阻止测试任务运行 migrate,根据我一直在阅读的内容,这应该是错误发生的地方.很奇怪,因为当我运行 migrate 时一切都很好.

I can figure out a workaround following this answer which prevents the test task from running migrate, which according to what I have been reading should be the point where the error happens. Odd enough, since when I run migrate everything is fine.

但是,我不希望采用厚颜无耻的解决方法,而是坚持按照设计的方式做事.此外,此错误可能暗示其他问题实际上是错误的,应该修复.

However, I would prefer not to resort to a cheeky workaround and stick to doing things as they are designed. Additionally, this error might be a hint that something else is actually wrong and ought to be fixed.

一些解决方案建议运行:

Some solutions suggest running:

python manage.py migrate auth
python manage.py migrate

但这无济于事,因为我的项目中没有待处理的迁移.

But that does nothing since there are no pending migrations in my project.

我该如何解决这个神秘的问题?

How can I solve this mysterious issue?


解决方案

如果您有任何具有 auth.User 外键的应用程序,请确保这些应用程序的初始迁移具有依赖 auth 应用:

If you have any apps which have foreign keys to auth.User, then make sure that the initial migrations for those apps have a dependency on the auth app:

class Migration(migrations.Migration):

    dependencies = [('auth', '__first__')]

相关文章