如何忽略特定迁移?

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

我有这样的迁移:

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0020_auto_20191023_2245'),
    ]

    operations = [
        migrations.AddField(
            model_name='agenda',
            name='theme',
            field=models.PositiveIntegerField(default=1),
        ),
    ]

但是会引发错误:

django.db.utils.ProgrammingError: column "theme" of relation "app_agenda" already exists

没问题,我把这个错误包装成这样:

from django.db import migrations, models, ProgrammingError


def add_field_theme_to_agenda(apps, schema_editor):
    try:
        migrations.AddField(
            model_name='agenda',
            name='theme',
            field=models.PositiveIntegerField(default=1),
        ),
    except ProgrammingError as e:  # sometimes it can exist
        if "already exists" not in str(e):
            raise


class Migration(migrations.Migration):
    dependencies = [
        ('app', '0020_auto_20191023_2245'),
    ]
    operations = [
        migrations.RunPython(add_field_theme_to_agenda),
    ]

这就像一个符咒,以下所有迁移都已完成。

我的问题是,每次我运行“ makemigrations”时,Django都会添加[[再次迁移(=我的问题顶部的迁移)。我猜这是因为在迁移中看不到它,因为我的代码使它变得模糊了。

如何通过迁移来避免这种情况(不要说诸如“此问题在您的数据库中,请更正您的数据库”之类的答案?)>

我有这样的迁移:类Migration(migrations.Migration):依赖关系= [('app','0020_auto_20191023_2245'),]操作= [migrations.AddField(...

django django-migrations
2个回答
1
投票

2
投票
© www.soinside.com 2019 - 2024. All rights reserved.