# Foreign keys in Ruby on Rails

**URL:** <https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/81316>\
**Category:** Rails\
**Created:** [November 18, 2005, 6:50pm UTC](https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/81316 "2005-11-18T18:50:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lieven\_dekeyzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bcef8e/32.png) [@lieven\_dekeyzer](https://www.ruby-forum.com/u/lieven_dekeyzer)\
**Post date:** [November 18, 2005, 6:50pm UTC](https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/81316/1 "2005-11-18T18:50:08Z")

</div>

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

Thanks for any hints,  
Lieven

---

<div class="post-metadata">

**Author:** ![lieven\_dekeyzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bcef8e/32.png) [@lieven\_dekeyzer](https://www.ruby-forum.com/u/lieven_dekeyzer)\
**Post date:** [November 18, 2005, 7:08pm UTC](https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/81316/2 "2005-11-18T19:08:16Z")

</div>

Lieven De Keyzer wrote:

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

Or better yet, only in the database.

You really want the database to take of these constraints since that is  
the piece of software that will be handing your data. Of course, you may  
add them to the model code but I think that would be redundant.

- Adam

---

<div class="post-metadata">

**Author:** ![lieven\_dekeyzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bcef8e/32.png) [@lieven\_dekeyzer](https://www.ruby-forum.com/u/lieven_dekeyzer)\
**Post date:** [November 18, 2005, 9:33pm UTC](https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/81316/3 "2005-11-18T21:33:12Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Nov 18, 2005, at 10:06 AM, Adam M. wrote:

> you may  
> add them to the model code but I think that would be redundant.

Putting them in the model makes error handling easier and more  
naturally expresses your domain model. Putting them in the database  
is easy, so why not; it’s a nice fallback.

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDfjnpAQHALep9HFYRAv5mAJ475shrES6ziYA6NLw6ECPhqBJOXgCePol6  
E/TJ2sl6y58B0pneix5obag=  
=/lzQ  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![lieven\_dekeyzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bcef8e/32.png) [@lieven\_dekeyzer](https://www.ruby-forum.com/u/lieven_dekeyzer)\
**Post date:** [November 18, 2005, 10:00pm UTC](https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/81316/4 "2005-11-18T22:00:20Z")

</div>

On 11/18/05, Adam M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the piece of software that will be handing your data. Of course, you may  
> add them to the model code but I think that would be redundant.
> 
> - Adam

That depends. I have an application that supports postgresql, mysql, and  
sqlite. Since sqlite doesn’t support foreign keys I have to put it in  
the  
model even though it’s redundant for postgresql and mysql.

Chris

---

<div class="post-metadata">

**Author:** ![lieven\_dekeyzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bcef8e/32.png) [@lieven\_dekeyzer](https://www.ruby-forum.com/u/lieven_dekeyzer)\
**Post date:** [November 18, 2005, 10:48pm UTC](https://www.ruby-forum.com/t/foreign-keys-in-ruby-on-rails/81316/5 "2005-11-18T22:48:40Z")

</div>

snacktime wrote:

> That depends. I have an application that supports postgresql, mysql,  
> and sqlite. Since sqlite doesn’t support foreign keys I have to put  
> it in the model even though it’s redundant for postgresql and mysql.

It does support foreign keys, just doesn’t enforce them.

[http://www.justatheory.com/computers/databases/sqlite/](http://www.justatheory.com/computers/databases/sqlite/)

see bottom of that page entitled “Enforce Foreign Key Integrity in  
SQLite with Triggers”. But on the other hand emulating FK constraints  
with Rails is not difficult.

- Adam
