gpt4 book ai didi

python - Django 测试失败,显示 'django.db.utils.ProgrammingError: relation "django_content_type“不存在”

转载 作者:行者123 更新时间:2023-12-05 03:46:53 25 4
gpt4 key购买 nike

我正在尝试为 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

最佳答案

我想通了。我没有跑makemigration <app>我的一个应用程序使用了 from django.contrib.contenttypes.models import ContentType模型。

不知何故,通过runserver 运行本地服务器并不重要, 但在运行 test 时没有通过命令。

关于python - Django 测试失败,显示 'django.db.utils.ProgrammingError: relation "django_content_type“不存在”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65166898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com