为什么我不能删除具有相关对象的模型实例? [英] Why can't I delete a model instance with related objects?

查看:114
本文介绍了为什么我不能删除具有相关对象的模型实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Django应用程序中删除一个用户对象,但是我不断得到一个 IntegrityError 删除 User 时的相关对象。相关对象如下所示:

  class取消关注(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)

但是,删除用户时,会收到以下错误消息:


IntegrityError:表auth_user上的更新或删除违反了core_unfollowing表上的外键约束source_id_refs_id_5b69e09fc6161c2a

DETAIL:Key( id)=(6439)仍然从表core_unfollowing中引用。


什么是交易?当相关的用户被删除时,不应自动删除相关的取消关注对象?为什么值得,我将 on_delete = models.CASCADE 添加到 Unfollowing.source Unfollowing.target (即使这是默认值),我仍然有相同的错误。

解决方案

这太奇怪了。以下是我将尝试的几件事:



首先,添加 on_delete = models.CASCADE ?如果没有,就不会有任何影响。您可以通过直接转储数据库模式来激活 ON DELETE CASCADE ,而不是从Django中获取数据。



似乎对我来说,这是一个db引擎相关的错误,而不是Django的。如果您是100%积极的 ON DELETE CASCADE 在auth_user表和相关表中激活,那么您可以尝试直接从您的sql客户端删除用户。如果它工作,并且同一个用户给Django(或类似的用户,给定您删除了第一个的事实),那么尝试转储SQL查询Django正在做,并从您的sql客户端重复。 p>

这是我要采取的方法。另外,检查是否与特定用户或每个用户发生这种情况,检查在下面的表中悬挂的引用。也许还有一个你曾经拥有的旧表,那是给你问题的一个。



我将把研究直接集中在数据库和一个sql客户端之后我设法让它在那里工作,回到Django。



这个更多的路径我会遵循,而不是一个答案,因为这似乎是一个非常具体的问题。



我希望这可能有所帮助。



祝你好运!


I'm trying to delete a User object in a Django app, but I keep getting an IntegrityError in a related object when deleting the User. The related object looks like this:

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)

But when deleting a User, I get an error like the following:

IntegrityError: update or delete on table "auth_user" violates foreign key constraint "source_id_refs_id_5b69e09fc6161c2a" on table "core_unfollowing"
DETAIL: Key (id)=(6439) is still referenced from table "core_unfollowing".

What's the deal? Shouldn't the related Unfollowing objects be deleted automatically when an associated User is deleted? For what it's worth, I added an explicit on_delete=models.CASCADE to both Unfollowing.source and Unfollowing.target (even though that's the default) and I still got the same error.

解决方案

That's weird indeed. Here's a couple of things I would try:

First, did you update the database schema after adding on_delete=models.CASCADE?. If not, it wouldn't do any effect. You can double check if ON DELETE CASCADE is activated by dumping the database schema directly, not figuring it out from Django.

Seems to me this is more a db engine related error than Django's. If you're 100% positive ON DELETE CASCADE is activated in auth_user table and the related tables, then you can try to delete a user directly from your sql client. If it works, and the same user give you trouble from Django (or a similar user, given the fact that you erased the first one), then try to dump the SQL query Django is doing and repeat it from your sql client.

This is the approach I would take. Also, check if this happens with a specific user or every user, check for hanging references in the Unfollowing tables. Maybe also there exist an old table you used to have and that's the one giving you problem.

I'll focus the research directly with the database and a sql client and after I managed to get it working there, move back to Django.

This is more the path I'll follow than an answer since this seems to be a very specific problem.

I hope this might help a little.

Good luck!

这篇关于为什么我不能删除具有相关对象的模型实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆