我正在尝试为一个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 (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
INSTALLED_APPS
中是否包含了django.contrib.contenttypes
? - crazychukz