# Foreign Keys: The "Rails Way" or Not?

**URL:** <https://www.ruby-forum.com/t/foreign-keys-the-rails-way-or-not/124650>\
**Category:** Rails\
**Created:** [December 31, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/foreign-keys-the-rails-way-or-not/124650 "2007-12-31T16:49:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![larry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@larry](https://www.ruby-forum.com/u/larry)\
**Post date:** [December 31, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/foreign-keys-the-rails-way-or-not/124650/1 "2007-12-31T16:49:54Z")

</div>

“Agile Web D. with Rails” by Dave T., page 322:

You can choose to to establish these [foreign-key] constraints if you  
want (and I personally recommend that you do), but the foreign-key  
support in Rails doesn’t need them.

“The Rails Way” by Obie F., page 296:

… we haven’t really touched on a subject of particular importance to  
many programmers: foreign-key constraints in the database. That’s  
mainly because use of foreign-key constraints simply isn’t the Rails  
way to tackle the problem of relational integerity.

I know there won’t be unanimous consensus on this topic, but is there  
a “Rails Way” concerning foreign keys? If “The Way” is not to use  
them, what do we use?

Thanks,  
Larry

---

<div class="post-metadata">

**Author:** ![larry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@larry](https://www.ruby-forum.com/u/larry)\
**Post date:** [January 11, 2008, 9:03am UTC](https://www.ruby-forum.com/t/foreign-keys-the-rails-way-or-not/124650/2 "2008-01-11T09:03:58Z")

</div>

Database constraints, when violated, shoot back an exception to the  
rails app at run time.

This is good for debugging and development. It’s like compiling in  
‘DEBUG’ mode. But this would be bad in production. Your customers  
will not be happy to see a rails error/exception page. They are  
ugly. On the other hand, if the app is coded ‘correctly’, it will  
never happen at run time unless something else (like a hardware  
failure) corrupts the database. Since it will ‘never’ happen, it is a  
moot point. If a tree falls in the forest…

On the other hand, how many apps are coded ‘correctly’? If your  
customers are complaining about nasty ‘exception’ pages, you can fix  
the problem more quickly than if you let the corruption continue to  
spread throughout the database.

On the other, other hand, if a true hardware glitch results in a rails  
exception page, the users will blame you. But a true hardware glitch  
is so rare that it probably is your fault anyway.

This is an engineering trade-off that depends… blah, blah, blah…

f

---

<div class="post-metadata">

**Author:** ![larry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@larry](https://www.ruby-forum.com/u/larry)\
**Post date:** [January 11, 2008, 4:04pm UTC](https://www.ruby-forum.com/t/foreign-keys-the-rails-way-or-not/124650/3 "2008-01-11T16:04:50Z")

</div>

I recently blogged about this in favor of using FKs -  
[http://softiesonrails.com/2008/1/9/you-don-t-need-foreign-key-constraints-in-rails](http://softiesonrails.com/2008/1/9/you-don-t-need-foreign-key-constraints-in-rails).

This was in response to a post on Softies on Rails.

---

<div class="post-metadata">

**Author:** ![larry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@larry](https://www.ruby-forum.com/u/larry)\
**Post date:** [January 20, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/foreign-keys-the-rails-way-or-not/124650/4 "2008-01-20T14:33:12Z")

</div>

On 20 Jan 2008, at 09:00, Thufir wrote:

> > database. Since it will ‘never’ happen, it is a moot point. If a
> 
> I would love to see FK constraints in a RDBMS, however, I, personally,  
> have never seen this. Maybe in Oracle? I wonder if it’s a pipe dream  
> (for it to work correctly).

Just about everyone does support FK constraints (including postgres,  
mysql (as long as you stick to innodb that is))

Fred

---

<div class="post-metadata">

**Author:** ![larry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@larry](https://www.ruby-forum.com/u/larry)\
**Post date:** [January 20, 2008, 10:05am UTC](https://www.ruby-forum.com/t/foreign-keys-the-rails-way-or-not/124650/5 "2008-01-20T10:05:51Z")

</div>

On Fri, 11 Jan 2008 00:03:13 -0800, fredistic wrote:

> problem more quickly than if you let the corruption continue to spread  
> throughout the database.

I would love to see FK constraints in a RDBMS, however, I, personally,  
have never seen this. Maybe in Oracle? I wonder if it’s a pipe dream  
(for it to work correctly).

-Thufir
