错误:更新或删除表“用户”违反外键约束“ fk_rails_03de2dc08c”;在表格“评论”中(ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_03de2dc08c" on table "comments")

13 IT屋

I am stuck on this error and not able to figure out whats wrong.

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

解决方案

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

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

我被这个错误困住了,无法找出问题所在。



铁路中断了! ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation:错误:在表"用户"上的更新或删除违反了表"注释"上的外键约束" fk_rails_03de2dc08c"详细信息:仍从表"注释"中引用键(id)=(11) 。 :从"用户"中删除。在"用户"中。" id" = $ 1#31


解决方案

像用户编号11这样的声音注释,因此不能删除该用户,因为这些注释仍指向该用户。这就是外键的含义-注释表(子表)上的条目被键入父表(用户)-无法删除父表,因为这样会留下孤立记录



首先删除用户11的注释,或更改外键,使其处于"删除级联"模式-删除父级将导致自动删除子级记录


本文地址:IT屋 » 错误:更新或删除表“用户”违反外键约束“ fk_rails_03de2dc08c”;在表格“评论”中