Should I use rails 4.2 to add foreign_key or not?

With the release of rails 4.2, the add_foreign_key method was introduced. As far as I understand, it is used as a combination of migration with model:references and add_index .

Suppose I use only PostreSQL. ( add_foreign_key limited by MySQL and PostreSQL). Should I stop using migration model:references with add_index and start using only add_foreign_key ? If yes / no, why? What are the benefits of the new add_foreign_key method? Should I swap places?

+6
source share
1 answer

Foreign key constraints can help with referential integrity (you cannot insert data belonging to a book that does not exist, for example). Foreign keys also provide referential integrity of the database, not the integrity of the application layer (model).

The Rails team considered this important enough that they now automatically create foreign keys whenever you use references when creating your migrations.

+3
source

Source: https://habr.com/ru/post/981649/


All Articles