撤消重复字段的迁移

问题描述 投票:0回答:1

我们在 Django Wagtail 模型中添加了一个重复字段,这使迁移系统感到困惑。

以下内容已添加到其中一个模型中,同时进行迁移,添加了该字段的单个实例:

    stream = fields.StreamField(
        who_fund_blocks,
        blank=True,
        verbose_name="Additional content",
    )
    stream = fields.StreamField(
        who_fund_blocks,
        blank=True,
        verbose_name="Additional content",
    )

这一切似乎都很好,项目继续进行,在其他地方添加了新的字段和迁移,没有任何问题。

然后重复的字段被发现并从模型页面中删除。

现在,如果我们尝试在某个模型上添加字段并为其进行迁移,则会创建迁移但未完成,尽管没有任何错误直接表明迁移失败:

(.venv)  development  ➜ app 🐟 manpy migrate
/root/.cache/pypoetry/virtualenvs/.venv/lib/python3.10/site-packages/wagtail/utils/widgets.py:10: RemovedInWagtail70Warning: The usage of `WidgetWithScript` hook is deprecated. Use external scripts instead.
  warn(
System check identified some issues:

WARNINGS:
?: (urls.W005) URL namespace 'freetag_chooser' isn't unique. You may not be able to reverse all URLs in this namespace
?: (urls.W005) URL namespace 'pagetag_chooser' isn't unique. You may not be able to reverse all URLs in this namespace
?: (urls.W005) URL namespace 'sectiontag_chooser' isn't unique. You may not be able to reverse all URLs in this namespace
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, core, csp, django_cron, donations, importers, sessions, submissions, taggit, taxonomy, users, wagtail_localize, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailsearchpromotions, wagtailusers
Running migrations:
  Applying core.0019_utilitypage_show_navigation_bar_and_more...

如果我们添加重复的

stream
字段并创建新的迁移,也会发生同样的事情,即迁移看起来运行正常,但没有确认勾选。

当我启动服务器时,我们得到:

You have 1 unapplied migration(s). Your project may not work properly until you apply the migration for app(s): core.
Run 'python manage.py migrate' to apply them.

数据库似乎已经有该字段,但迁移系统想要尝试再次添加它但不能,这会阻止所有其他迁移。

如果我注释掉两个

stream
字段,则可以进行迁移,但站点会在数据库期望存在
stream
字段的页面上中断。我想我可以删除
stream
字段并手动删除数据库中的
stream
字段,尽管这样做会丢失一些数据,而且我更愿意使用 Django 和迁移来修复此问题。

知道如何取消选择它吗?

python django database migration wagtail
1个回答
0
投票

在没有看到您的迁移文件和数据库的情况下提供建议有点困难。听起来你的数据库表只有一份

stream
,对吧?因此,一旦删除模型文件中的重复项,数据库表和您的 python 模型就会匹配。

假设这是您的情况,那么我会编辑您的旧迁移文件以删除重复项。然后尝试在生产数据库的副本上运行

migrate
makemigrations
+
migrate
,直到您对一切按您希望的方式运行感到满意为止。

如果您的表有重复的列,我的第一个问题是怎么会发生这种情况,第二个问题是您使用的是什么数据库?我相当确定数据库中不能有重复的列,但如果是这样,这将是一个更长的对话。

© www.soinside.com 2019 - 2024. All rights reserved.