By release of rails 4.2 add_foreign_key method was introduced. As far as I understand it is used as combo of migration with model:references
and add_index
.
Let's say I only use PostreSQL. (add_foreign_key
is limited to MySQL and PostreSQL). Should stop using migration model:references
with add_index
and start using add_foreign_key
only?. If yes/no, why?. What are benefits of new add_foreign_key
method? Is it worth to swap?
Foreign key constraints can help with referential integrity (you can't insert data belonging to a book that doesn't exist for example). Foreign keys also provide database level referential integrity as opposed to application level (model validation) integrity.
The Rails team felt it important enough that they now automatically create foreign keys whenever you use references in generating your migrations.