具有两个m2m字段的模型上的IntegrityError

时间:2017-09-19 05:40:00

标签: python django postgresql many-to-many django-orm

这就是我的模型:

class Interaction(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    author = models.ForeignKey(Staff)
    datetime = models.DateTimeField()
    tags = models.ManyToManyField(Interaction_Type)
    comments = models.TextField()
    related_clients = models.ManyToManyField(Client, related_name="interactions", blank=True)
    related_staff = models.ManyToManyField(Staff, related_name="interactions", blank=True)
    active = models.BooleanField(default=False)

当我尝试保存此模型的实例时,我收到此错误:

django.db.utils.IntegrityError: insert or update on table "interactions_interaction_related_clients" violates foreign key constraint "interact_interaction_id_4927567c_fk_interactions_interaction_id"
DETAIL:  Key (interaction_id)=(85eb4cff-1be8-45f7-af40-459418ec6364) is not present in table "interactions_interaction".

感觉就像Django在写入主要的Interaction,Staff和Client表之前尝试写入数据透视表。

对此有任何补救措施吗?

0 个答案:

没有答案