【发布时间】: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