如果父记录不存在,如何删除所有子记录。
在迁移期间,我收到此错误。
utils.IntegrityError: Problem installing fixtures: insert or update on table "girvi_transactionauditlogentry" violates foreign key constraint "girv
ionauditloge_credit_id_d8df902_fk_girvi_credit_id"
ey (credit_id)=(5) is not present in table "girvi_credit".
答案 0 :(得分:1)
写一个小脚本:
class A(models.Model):
name = models.CharField(max_length=10)
# ...
class B(models.Model):
parent = models.ForeignKey(A)
# ...
脚本:
for b in B.objects.all():
if not b.parent:
b.delete()
然后再次运行迁移