RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!

Five tips for adding foreign keys to existing apps

You’re convinced foreign keys are a good idea, but how should you retroactively add them to your production Rails application? Here are some tips to help you out.

Comments

Good points. It’s ridiculous that rails community is so unaware of database constraints. I would recommend Automatic foreign key gem which makes working with foreign keys extremely easy.

I doubt “rails is afraid” of foreign key constraints, and it is not a no brainer to use them. There’s a basic philosophy that says the more you use DB-specific features, the more you’re tied to that technology. By viewing the DB as a dumb data store, and having data-integrity managed by your application, the more portable and flexible the application as a whole is. Given Rails opinion about db agnostics, I’d bet that’s the real reason you don’t see FK constraints built-in. That and MyISAM tables don’t support it!

Frankly, how often do you change such a crucial component like database in your projects? In my opinion db-agnosticism isn’t an issue. I agree that maintenance of db may be sometimes hard. Although db-constraints were invented to keep consistency and there is no possibility to do it right on application level. Things like unique constraints, foreign keys or triggers serve us to ensure that we don’t have orphaned or dirty data. Doing it from scratch on app-level is reinventing the wheel.

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in