【发布时间】:2015-11-19 05:30:30
【问题描述】:
我进行了相当长的数据迁移,以纠正早期的错误迁移,其中某些行创建不正确。我正在尝试根据旧列将值分配给新列,但是,有时这会导致完整性错误。发生这种情况时,我想丢弃导致完整性错误的那个
这是一个代码sn-p:
def load_data(apps, schema_editor):
MyClass = apps.get_model('my_app', 'MyClass')
new_col_mapping = {old_val1: new_val1, ....}
for inst in MyClass.objects.filter(old_col=c):
try:
inst.new_col = new_col_mapping[c]
inst.save()
except IntegrityError:
inst.delete()
然后在我的Migration类的操作中我做
operations = [
migrations.RunPython(load_data)
]
运行迁移时出现以下错误
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block
我有这种感觉
with transaction.atomic():
某处是我的解决方案,但我不确定正确的位置在哪里。更重要的是,我想了解为什么这是必要的
【问题讨论】:
标签: django data-migration