gpt4 book ai didi

python - PyTest-Django 因缺少 django_migration 表而失败

转载 作者:太空狗 更新时间:2023-10-29 17:20:32 26 4
gpt4 key购买 nike

我正在尝试将 pytest-django 添加到我当前的 pytest3/Django1.7 环境中。

目前我们还没有使用该插件,并且一直受到某些测试之间的共享状态的困扰

在我收到以下错误消息时,一切似乎都在视觉上看起来不错,并且测试似乎一直通过,直到结束:

request = <SubRequest '_django_db_marker' for <Function 'test_filter_recurring_outside_sync_window'>>

@pytest.fixture(autouse=True)
def _django_db_marker(request):
"""Implement the django_db marker, internal to pytest-django.

This will dynamically request the ``db`` or ``transactional_db``
fixtures as required by the django_db marker.
"""
marker = request.keywords.get('django_db', None)
if marker:
validate_django_db(marker)
if marker.transaction:
getfixturevalue(request, 'transactional_db')
else:
getfixturevalue(request, 'db')

ve/lib/python2.7/site-packages/pytest_django/plugin.py:376:


self = <django.db.backends.sqlite3.base.SQLiteCursorWrapper object at 0x11976a478>
query = 'SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"', params = ()

def execute(self, query, params=None):
if params is None:
return Database.Cursor.execute(self, query)
query = self.convert_query(query)
> return Database.Cursor.execute(self, query, params)
E OperationalError: no such table: django_migrations

ve/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py:485: OperationalError

我尝试在 conftest.py 中使用 ensure_schema 创建表。我已经尝试了 --nomigrations--create-dbpytest 的所有选项。

我猜这是我在遗留系统中遇到的一个奇怪的配置问题,但我不确定从哪里开始寻找。有人有建议吗?

最佳答案

看起来可能是迁移的问题。

运行 ./manage.py schemamigration research --auto 显示大多数字段没有指定任何默认值。

接下来,运行 ./manage.py schemamigration research --init 然后运行 ​​./manage.py migrate research

这在我创建表格后对我有用:

python manage.py migrate --run-syncdb

注意:不要忘记先运行 python makemigrations,即 python manage.py makemigrations {患者模型所在的应用程序的名称}

Helpful tips: There is a table generated by django called django_migrations which keeps track of what migrations have been applied. If you delete your migrations, re-generate them and try to migrate without deleting the records from the table, then django will think it already applied them. You should never delete your migrations, as it will cause django to get confused.

You can skip the migrations step if you are actively developing If you are actively developing and want to skip the entire migrations system you can, but once you start using migrations, never delete them. Here is what I use while developing a new project:

dropdb mydb && createdb mydb && python manage.py migrate --run-syncdb && python manage.py loaddata initial

首先,它会删除数据库和所有数据。然后它创建一个空的 一。 --run-syncdb 生成模式和 loaddata 加载 来自 fixtures file 的数据.

因此,如果您仍在开发并且可以删除所有数据并移动 你关心的东西到一个fixtures文件,然后你可以删除你所有的 迁移文件夹。然后,您可以在每次更改时运行上面的命令 模型。

关于python - PyTest-Django 因缺少 django_migration 表而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41770334/

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