我正在尝试删除UserDjango 应用程序中的一个对象,但是IntegrityError在删除User. 相关对象如下所示:
class Unfollowing(models.Model):
source = models.ForeignKey(User, related_name='unfollowings_by')
target = models.ForeignKey(User, related_name='unfollowings_of')
created_on = models.DateTimeField(auto_now_add=True)
但是在删除 a 时User,我收到如下错误:
IntegrityError:更新或删除表“auth_user”违反了表“core_unfollowing”上的外键约束“source_id_refs_id_5b69e09fc6161c2a”
详细信息:键(id)=(6439)仍然从表“core_unfollowing”中引用。
这是怎么回事?Unfollowing删除关联对象时不应该自动删除相关对象User吗?对于它的价值,我on_delete=models.CASCADE为两者添加了一个显式Unfollowing.source和Unfollowing.target(即使这是默认设置),我仍然得到同样的错误。