# Foreign keys in Ruby on Rails

**URL:** <https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/48285>\
**Category:** Ruby\
**Created:** [November 18, 2005, 4:09pm UTC](https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/48285 "2005-11-18T16:09:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![spambak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@spambak](https://www.ruby-forum.com/u/spambak)\
**Post date:** [November 18, 2005, 4:09pm UTC](https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/48285/1 "2005-11-18T16:09:37Z")

</div>

Should the foreign key constraints only be declared in the Model code of  
the application, or in both the code and the database?

---

<div class="post-metadata">

**Author:** ![spambak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@spambak](https://www.ruby-forum.com/u/spambak)\
**Post date:** [November 18, 2005, 4:46pm UTC](https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/48285/2 "2005-11-18T16:46:44Z")

</div>

spambak wrote:

> Should the foreign key constraints only be declared in the Model code of  
> the application, or in both the code and the database?

Why not ask on the Rails list?

If your database can be used from other programs, you want the  
constraints to exist in the database (so they can’t be broken by other  
software that ignores them). Otherwise, it seems to me, it’s just  
overhead.

You could say that about any validation logic you write into your model  
(or get for free from ActiveRecord) – perhaps it should be duplicated  
by validation triggers in your database, so that other code can’t mess  
it up – but when you do that, you seem to lose a large amount of the  
advantage of Rails over other web-database frameworks.

Good luck…
