ForeignKeyViolation: update or delete on table violates foreign key constraint, by Christian Fei
Add a Foreign Key Constraint ON DELETE CASCADE
Today at work I encountered an interesting topic and I want to document it a bit. We couldn’t delete the associations of associations because we got the following error:
PG::ForeignKeyViolation at /authors/2 ERROR: update or delete on table "posts" violates foreign key constraint "fk_rails_5653572c8a" on table "comments" DETAIL: Key (id)=(1) is still referenced from table "comments".
This means that ActiveRecord tried to delete the posts, while the comments still references the posts.
This violates the referential integrity we defined in the migration.
We didn’t update it to
RESTRICT as default.
If you’re encountering the same error in Rails, you’ll need to add the remove the foreign key and add it again with the referential integrity options set to
remove_foreign_key :comments, :posts remove_foreign_key :posts, :authors add_foreign_key :comments, :posts, on_delete: :cascade add_foreign_key :posts, :authors, on_delete: :cascade
Action Controller Parameters, by Christian Fei
require ensures that the defined parameters are present, else it throws an error.
permit on the other hand “filters” the parameters and permits/whitelists only the specified ones.