错误:更新或删除表“用户"违反外键约束"fk_rails_03de2dc08c";在表格“评论"中 [英] ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_03de2dc08c" on table "comments"
问题描述
我被这个错误困住了,无法找出问题所在.
I am stuck on this error and not able to figure out whats wrong.
铁轨中止了! ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation:错误:表用户"上的更新或删除违反了表注释"上的外键约束"fk_rails_03de2dc08c"详细信息:仍从表注释"中引用键(id)=(11) . :从用户"中删除,在用户"中."id" = $ 1#31
rails aborted! ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_03de2dc08c" on table "comments" DETAIL: Key (id)=(11) is still referenced from table "comments". : DELETE FROM "users" WHERE "users"."id" = $1 #31
推荐答案
像11号用户这样的声音发出了一些注释,因此该用户无法删除,因为这些注释仍指向该用户.这就是外键-注释表(子表)上的条目被键入父表(用户)-无法删除父表,因为这样会留下孤立记录
Sounds like user number 11 made some comments and the user hence cannot be deleted because those comments still refer to that user. This is what a foreign key is- entries on the comments table (a child table) are keyed to the parent table (user) - the parent cannot be deleted because that would leave orphan records
首先删除用户11的评论,或更改外键,使其处于删除级联"模式-删除父级将导致自动删除子级记录
Delete user 11's comments first, or change the foreign key so it's "on delete cascade" mode - deletion of the parent will cause automatic deletion of the child records
这篇关于错误:更新或删除表“用户"违反外键约束"fk_rails_03de2dc08c";在表格“评论"中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!