Django 外键约束甚至你 on_delete=CASCADE

Django foreign key constraint on drop even thou on_delete=CASCADE

我看不出这有什么问题,

class Agenda(models.Model):
    ...

class AgendaResource(PolymorphicModel):
    agenda = models.ForeignKey(
        Agenda, related_name="resources", on_delete=models.CASCADE
    )
    comment = models.TextField(null=True)

class PreemptiveMeasureResource(AgendaResource):
    resource = models.ForeignKey(
        PreemptiveMeasure, on_delete=models.SET_NULL, null=True
    )
    ...

当我尝试删除议程时,即 Agenda.objects.get(pk=2).delete() 我遇到了这个问题:

update or delete on table "school_health_agendaresource" violates foreign key constraint "school_health_preemp_agendaresource_ptr_i_222e2e2c_fk_school_he" on table "school_health_preemptivemeasureresource"
DETAIL:  Key (id)=(2) is still referenced from table "school_health_preemptivemeasureresource"

我不明白的是什么?我猜这与继承有关?

这是 django-polymorphic 的问题,请参阅问题 here, here and here

您可以尝试将以下内容添加到您的 AgendaResource 模型中作为解决方法:

class AgendaResource(PolymorphicModel):
    ...
    non_polymorphic = models.Manager()

    class Meta
        base_manager_name = 'non_polymorphic'