Are you using foreign keys?

Hi Guys,

When you use ActiveRecord relationship, there is always a place to put foreign key on your table, e.g., id and foo_id. However, from rails perspective, foreign keys are not required as long as data could be associated.

I would like know your design decision on database foreign keys. Is it better to have it? Or just leave it to speed up your development?

Thanks, Glenn

I normally use them via this plugin:

   http://www.redhillonrails.org/#foreign_key_migrations

You get indices as a side-effect (don't know whether that applies to all databases) and they help getting your :dependents right in has_* declarations.

-- fxn

Hi Everyone,

Thank you very much for your postings. It really helped me to start a new project.

Ilan, Thanks for your clarification of the terminology. Yes, I was wondering if foreign key constrain should be defined on database level.

As Josh pointed, it we can check referential integrity on application level. We used to use database for everything such as managing transaction, validating data, user authentication, etc. However, it appears that current trend is to put most staff in application level and let database to take care persistence only. My work place still have lots of legacy applications sitting top of of the database. It would be good idea to have constrains in both application and database.

Xavier, thank you so much for showing me a nice plugin. I will definatelly look into the plugin. It's will be very nice to have automated solution for creating foreign key constrains.

Glenn