I am developing several things in Ruby on Rails, and I have presently got several models with associations together. Now, the models specify the relations, and so i realize that RoR will enforce integrity, but how about in the DB level ?

Do others setup foreign key associations in DB tables ? and when so, how ?, there does not appear to become in whatever way to createOrruin a db relationship inside a migration (maybe using raw SQL)

Thanks

Paul.

Here is a guide regarding how to get it done: http://seb.box.re/2006/7/29/foreign-key-and-rails-migration

There's additionally a wordpress plugin with this here: http://github.com/harukizaemon/foreign_key_migrations/tree/master

However, Rails doesn't easily support foreign secrets in migrations for any reason. Essentially they are not necessarily necessary when utilizing ActiveRecord.

Here is a good explanation of why they aren't necessary as well as their usage is frustrated in rails: http://www.motionstandingstill.com/i-dont-use-foreign-key-constraints-with-rails/2008-10-17/

Opinions differ about this subject. There is a good discussion here: http://forum.softiesonrails.com/forums/3/topics/138

There is not a method to get it done in the migration lacking using SQL, meaning:

  1. It's DB-specific
  2. Make use of SQL

The very first is not really that large an offer (how frequently would you switch databases on the project anyway?), and the second reason is just a fact of existence. So, rely on them if you would like.

Incidentally this stuff ought to always be setup in the database level. You will find different ways to gain access to and alter data within the database aside from the application. You shouldn't set these kinds of rules within the application unless of course you would like useless data. Everything that discuss data integrity should be in the database level even when you need to (GASP) use SQL.