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

时间:2017-10-13 09:12:10

标签: python django migration django-1.8

我最近将Django升级到1.8并建立了一个新的开发数据库,​​以便重新开始。迁移和依赖关系进展顺利,安全通常会出现错误并最终解决问题。该应用程序现在正在本地工作。

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

python manage.py test

这是我得到的错误:

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

毋庸置疑,Django的auth模块确实已在应用程序中安装和迁移,因此我不确定发生了什么。

以下是完整的堆栈跟踪,以防你需要查看它,但它没有说任何东西,甚至远程帮助我找出导致此错误的原因:

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:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\test.py", line 30, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\test.py", line 74, in execute
    super(Command, self).execute(*args, **options)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\test.py", line 90, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\test\runner.py", line 210, in run_tests
    old_config = self.setup_databases()
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\test\runner.py", line 166, in setup_databases
    **kwargs
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\test\runner.py", line 370, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\db\backends\base\creation.py", line 368, in create_test_db
    test_flush=not keepdb,
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\migrate.py", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\migrate.py", line 318, in sync_apps
    cursor.execute(statement)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\db\utils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\dabadaba\Envs\django18\lib\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

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

但是,我宁愿不采取厚颜无耻的解决方法,坚持按照设计的方式做事。此外,这个错误可能暗示其他东西实际上是错误的,应该修复。

有些解决方案建议运行:

python manage.py migrate auth
python manage.py migrate

但由于我的项目中没有待定的迁移,因此无效。

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

2 个答案:

答案 0 :(得分:1)

如果您有任何具有auth.User外键的应用,请确保这些应用的初始迁移在auth应用上有dependency

class Migration(migrations.Migration):

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

答案 1 :(得分:0)

我遇到了同样的问题,当我迁移了项目中包含的一个应用程序但该迁移不会自动执行时,终于解决了。

因此,只需检查您的应用,然后手动执行./manage.py makemigrations YOUR_APP_NAME./manage.py migrate