更新或删除表“ users_user”违反外国钥匙约束“ articles_article_users_user_id”
我正在使用Django V4。或反之亦然,所以我在文章模型中引用了用户on_delete = delete.do_nothing
,但是当我尝试删除用户时,我会遇到此错误我以为是吗?我如何通过这件事并实现我倾向于做的事情而不会限制错误?谢谢
class Article(models.Model):
title = models.CharField(max_length=355)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING,
related_name='articles')
.....
I am working with Django v4.* and I have linked 2 table/model Users
and Articles
and my aim is when user is deleted I don't want to delete the article or vice versa, so I referenced user on_delete=models.DO_NOTHING
in the Article model, but I am getting this error when I tried to delete a user, it seems to my that DO_NOTHING is not working the way I thought it does?How I can pass this and achieve what I tend to do without getting constrain error? Thanks
class Article(models.Model):
title = models.CharField(max_length=355)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING,
related_name='articles')
.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在大多数DB中参考现有的PK。如果您想在删除用户后离开文章,则应该执行这样的操作:
这样,当您删除参考用户时,作者将被设置为db中的null。
You can't reference not existing PK in most of DBs. If you want to leave Article after deleting User you should do something like that:
This way, when you are deleting referenceed user, author will be set to null in DB.