我有这样的迁移:
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(...