我正在尝试为Django项目编写和运行测试,但运行
$ python manage.py test apps/actions/tests
给出以下错误:
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
只有在我尝试运行测试时才会发生这种情况。运行python manage.py runserver
没有任何错误,一切正常。我用的是Django 3.1.3
以下是完整的traceback:
Traceback (most recent call last):
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "django_content_type" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Kamil\Projekty\random\manage.py", line 22, in <module>
main()
File "C:\Users\Kamil\Projekty\random\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\commands\test.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\test\runner.py", line 695, in run_tests
old_config = self.setup_databases(aliases=databases)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\test\runner.py", line 614, in setup_databases
return _setup_databases(
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\test\utils.py", line 170, in setup_databases
connection.creation.create_test_db(
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\db\backends\base\creation.py", line 72, in create_test_db
call_command(
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\__init__.py", line 168, in call_command
return command.execute(*args, **defaults)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\commands\migrate.py", line 214, in handle
self.sync_apps(connection, executor.loader.unmigrated_apps)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\core\management\commands\migrate.py", line 352, in sync_apps
self.stdout.write(' Running deferred SQL...')
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\db\backends\base\schema.py", line 115, in __exit__
self.execute(sql)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\db\backends\base\schema.py", line 142, in execute
cursor.execute(sql, params)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Kamil\Projekty\random\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
我已经尝试删除和重新创建迁移。我的INSTALLED_APPS
中有django.contrib.contenttypes
。Traceback并没有指向代码中的任何特定位置,所以我不知道该在哪里查找。
下面是python manage.py showmigrations
的输出。
actions
[X] 0001_initial
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
[X] 0010_alter_group_name_max_length
[X] 0011_update_proxy_permissions
[X] 0012_alter_user_first_name_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
guardian
[X] 0001_initial
[X] 0002_generic_permissions_index
integrations
[X] 0001_initial
links
[X] 0001_initial
sessions
[X] 0001_initial
triggers
[X] 0001_initial
users
[X] 0001_initial
workers
[X] 0001_initial
3条答案
按热度按时间56lgkhnf1#
我想明白了我没有为我的一个使用
from django.contrib.contenttypes.models import ContentType
模型的应用程序运行makemigration <app>
。不知何故,当通过
runserver
运行本地服务器时,它没有关系,但当运行test
命令时,它没有通过。pnwntuvh2#
这也可能意味着,数据库是不可访问的,假设我们重写DiscoverTestRunner类的setup_database方法,我们将遇到相同的错误。
pokxtpni3#
与OP不直接相关,但以下 edge case 导致了完全相同的错误和追溯:
在我们的一个遗留测试套件中,定义了一个“test-only”模型,* 没有任何迁移 *,但是有一个指向
ContentType
的外键和一个GenericForeignKey
(一个泛型关系)。这个仅测试的模型显然依赖于django_content_type
表。出于某种原因,如果我们使用本地SQLite数据库(在内存中)运行测试,这不是问题:所有测试运行正常。
然而,在切换到本地PostgreSQL数据库后,我们在尝试运行测试时得到错误:
像OP一样,
manage.py migrate
和runserver
可以正常工作。我们还确认django可以创建和删除测试数据库,因此PostgreSQL数据库配置和访问权限都很好。当Django在syncdb阶段尝试创建测试数据库表时,问题就出现了,因为Django首先尝试为没有迁移的模型创建表:在我们的例子中,其中一个 * 没有迁移的模型 * 实际上依赖于
ContentType
模型,它 * 确实 * 需要迁移。一种解决方法是为仅测试模型创建迁移,例如如所描述的here。
我还不确定为什么在使用SQLite时不会出现这个错误。
(Django 3.2,psycopg2 2.8,PostgreSQL 12)