【问题标题】:Why am I getting a 'ProgrammingError: relation "auth_user" does not exist' exception when running tests?为什么在运行测试时会出现“ProgrammingError:关系“auth_user”不存在”异常?
【发布时间】:2021-10-08 12:35:00
【问题描述】:

最近我将一个 Django 项目从 1.9.1 版本迁移到 3.2.7。

现在我正在尝试编写一些新的测试,但我收到了这个错误:

# python manage.py test
Creating test database for alias 'default'...
Got an error creating the test database: database "test_django" already exists

Type 'yes' if you would like to try deleting the test database 'test_django', or 'no' to cancel: yes
Destroying old test database for alias 'default'...
Traceback (most recent call last):
  File "/opt/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "auth_user" does not exist

我知道这是因为目前我没有任何“迁移”目录,因为我克隆了 git 存储库,并且当 Django 项目在 1.9.1 版本中运行时数据库已经存在。

我读过:

他们都建议运行迁移,但是:

# python manage.py makemigrations 
No changes detected
# python manage.py makemigrations auth
No changes detected in app 'auth'

同样,我认为这是因为数据库架构在升级到 3.2 之前就已经存在。

我似乎无法解决运行迁移方法的问题。

是否有其他方法可以解决此问题,或者即使数据库已经存在并且已同步(并且可能是伪造的)也强制生成迁移?

【问题讨论】:

  • 重用测试数据库不是一个选项吗?你可以用python manage.py test --keepdb
  • django.contrib.admin 是否在您的测试使用的 settings.py 文件的 INSTALLED_APPS 中?
  • @BrianDestura 这是一个选项,但我希望能够在每次运行时运行重新创建数据库的测试。如果最后我没有办法,我会保留数据库。
  • @aaron 我假设它正在使用项目的settings.py 文件(我没有覆盖任何东西),所以是的,django.contrib.admin 被包括在内。
  • 不幸的是,要重新创建您需要迁移的数据库。只是好奇,当你克隆的时候,为什么不包括迁移?

标签: django django-tests django-3.2


【解决方案1】:

如果错误是由于迁移造成的,您可以跳过迁移 使用以下 django 库运行测试时出错

django-test-without-migrations (pip install django-test-without-migrations)

安装库并将其添加到 INSTALLED_APPS (settings.py)

Then run, python manage.py test --nomigrations 

参考:https://pypi.org/project/django-test-without-migrations/

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2016-10-17
    • 1970-01-01
    相关资源
    最近更新 更多