ERROR: update or delete on table “users” violates foreign key constraint “fk_rails_03de2dc08c” on table “comments”

最后都变了- 提交于 2019-12-24 19:41:21

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/47624334/error-update-or-delete-on-table-users-violates-foreign-key-constraint-fk-rai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!