开发者

Foreign Key Relationship in Rails

开发者 https://www.devze.com 2022-12-31 05:23 出处:网络
I am a beginner in Rails and I read that the Rails enforces the foreign key relationships at the model level and also at the databas开发者_C百科e level in the migration file, while creating the table.

I am a beginner in Rails and I read that the Rails enforces the foreign key relationships at the model level and also at the databas开发者_C百科e level in the migration file, while creating the table. Is it really necessary and what kind of advantage does it provide


It is not strictly required, however it is a good idea both from the perspective of documenting that the relationship exists, and to help to maintain database integrity and bring business logic errors to light more quickly. It is also generally a good idea to follow rails conventions rather than swim against the tide, unless you have a very compelling reason to do so.


I highly recommend creating foriegn keys, you can look at my answer here.

Short answer: can increase performance due to indexes created by foreign keys. Reduces invalid data, which can lead to cleaner code.

0

精彩评论

暂无评论...
验证码 换一张
取 消