gpt4 book ai didi

Django 单元测试 - 无法创建 django_migrations 表

转载 作者:行者123 更新时间:2023-11-29 12:24:23 24 4
gpt4 key购买 nike

我正在尝试编写一些单元测试并使用 manage.py test 运行它们,但由于某些原因脚本无法创建 django_migrations 表。

这是完整的错误:

Creating test database for alias 'default'...
Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 83, in _execute
return self.cursor.execute(sql)
psycopg2.ProgrammingError: no schema has been selected to create in
LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA...
^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\recorder.py", line 55, in ensure_schema
editor.create_model(self.Migration)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 298, in create_model
self.execute(sql, params or None)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 117, in execute
cursor.execute(sql, params)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 83, in _execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: no schema has been selected to create in
LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA...
^


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\commands\test.py", line 26, in run_from_argv
super().run_from_argv(argv)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\commands\test.py", line 59, in handle
failures = test_runner.run_tests(test_labels)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\test\runner.py", line 601, in run_tests
old_config = self.setup_databases()
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\test\runner.py", line 548, in setup_databases
self.parallel, **kwargs
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\test\utils.py", line 176, in setup_databases
serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\creation.py", line 68, in create_test_db
run_syncdb=True,
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 141, in call_command
return command.execute(*args, **defaults)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
fake_initial=fake_initial,
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 91, in migrate
self.recorder.ensure_schema()
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\recorder.py", line 57, in ensure_schema
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (no schema has been selected to create in
LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA...
^
)

我尝试运行这些建议的 GRANT 语句 here :

grant usage on schema public to <username>;  
grant create on schema public to <username>;

但我仍然收到相同的消息。有什么办法可以解决这个问题吗?可能是 search_path 问题?

更新

~ python manage.py makemigrations users
Migrations for 'users':
users\migrations\0001_initial.py
- Create model MyUser

~ python manage.py migrate
Operations to perform:
Apply all migrations: auth, contenttypes, sessions, users
Running migrations:
No migrations to apply.

我在执行 migrate 之前尝试了 makemigrations(呃!),但我仍然遇到与以前相同的错误。迁移甚至得到应用了吗?我还应该提到我的 models.py 只有一个模型/表,它不是由 Django “管理”的。

Bueller...Bueller...Beuller...有人吗?

最佳答案

对于同样的错误,我做了一些不同的事情,没什么不同,但我直接去了 psql 服务器(在我的例子中)。它发生在开发过程中。所以,我没有意识到可能的数据丢失。选择您的数据库。然后,创建架构。

CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;

我从https://stackoverflow.com/a/13823560/10860596得到了答案.因为,我遇到了问题,因为我删除了所有表格。

关于Django 单元测试 - 无法创建 django_migrations 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48589076/

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