Hi! My name is Christian, an Agile Software Developer, living in Trento (IT).
I am currently working at XPeppers.
Read more about me.

ForeignKeyViolation: update or delete on table violates foreign key constraint

,
by

tldr;

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 CASCADE, but 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 CASCADE:

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

Reference: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_foreign_key

Action Controller Parameters

,
by

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.


Reference: http://api.rubyonrails.org/classes/ActionController/Parameters.html

ignore code in istanbul

,
by

https://github.com/gotwarlost/istanbul#ignoring-code-for-coverage

tid - today i discovered

,
by

i want to try tracking things i discover (like time based bookmarks, ideas, findings) in a data structure, simple enough to be used daily. Here is my tid.yml with which this site is created (kinda meta, isn’t it?).