# Are you using foreign keys?

**URL:** <https://www.ruby-forum.com/t/are-you-using-foreign-keys/110589>\
**Category:** Rails\
**Created:** [July 27, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/are-you-using-foreign-keys/110589 "2007-07-27T21:06:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [July 27, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/are-you-using-foreign-keys/110589/1 "2007-07-27T21:06:40Z")

</div>

Hi Guys,

When you use ActiveRecord relationship, there is always a place to put  
foreign key on your table, e.g., id and foo\_id. However, from rails  
perspective, foreign keys are not required as long as data could be  
associated.

I would like know your design decision on database foreign keys. Is  
it better to have it? Or just leave it to speed up your development?

Thanks,  
Glenn

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [July 27, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/are-you-using-foreign-keys/110589/2 "2007-07-27T22:16:01Z")

</div>

Glenn wrote:

> Hi Guys,
> 
> When you use ActiveRecord relationship, there is always a place to put  
> foreign key on your table, e.g., id and foo\_id. However, from rails  
> perspective, foreign keys are not required as long as data could be  
> associated.
> 
> I would like know your design decision on database foreign keys. Is  
> it better to have it? Or just leave it to speed up your development?
> 
> Thanks,  
> Glenn

Glenn,

Are you referring to “foreign keys” or “foreign key constraints”? The  
former is certainly needed by ActiveRecord/Rails for any non trivial  
relationship between 2 tables. Leaving out foreign keys is not even an  
option when working with relational entities.

Perhaps you meant foreign key constraints? Currently, their use  
requires that you know vendor specific details of your db (which is not  
usually a concern for most). Anyways, in my opinion, you are right in  
that you can usually omit them until your DBA comes back from vacation  
or you wake up in a pool of sweat knowing that you could have done more  
to protect the integrity of your data.

HtH

ilan

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [July 28, 2007, 1:31am UTC](https://www.ruby-forum.com/t/are-you-using-foreign-keys/110589/3 "2007-07-28T01:31:19Z")

</div>

El Jul 27, 2007, a las 9:06 PM, Glenn  
escribió:

> I would like know your design decision on database foreign keys. Is  
> it better to have it? Or just leave it to speed up your development?

I normally use them via this plugin:

[http://www.redhillonrails.org/#foreign\_key\_migrations](http://www.redhillonrails.org/#foreign_key_migrations)

You get indices as a side-effect (don’t know whether that applies to  
all databases) and they help getting your :dependents right in has\_\*  
declarations.

– fxn

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [August 2, 2007, 3:54am UTC](https://www.ruby-forum.com/t/are-you-using-foreign-keys/110589/4 "2007-08-02T03:54:51Z")

</div>

Hi Everyone,

Thank you very much for your postings. It really helped me to start a  
new project.

Ilan, Thanks for your clarification of the terminology. Yes, I was  
wondering if foreign key constrain should be defined on database  
level.

As Josh pointed, it we can check referential integrity on application  
level. We used to use database for everything such as managing  
transaction, validating data, user authentication, etc. However, it  
appears that current trend is to put most staff in application level  
and let database to take care persistence only. My work place still  
have lots of legacy applications sitting top of of the database. It  
would be good idea to have constrains in both application and  
database.

Xavier, thank you so much for showing me a nice plugin. I will  
definatelly look into the plugin. It’s will be very nice to have  
automated solution for creating foreign key constrains.

Glenn

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [July 28, 2007, 12:48am UTC](https://www.ruby-forum.com/t/are-you-using-foreign-keys/110589/5 "2007-07-28T00:48:14Z")

</div>

Ilan B. wrote:

> Glenn wrote:
> 
> > When you use ActiveRecord relationship, there is always a place to put  
> > foreign key on your table, e.g., id and foo\_id. However, from rails  
> > perspective, foreign keys are not required as long as data could be  
> > associated.
> > 
> > I would like know your design decision on database foreign keys. Is  
> > it better to have it? Or just leave it to speed up your development?
> 
> Are you referring to “foreign keys” or “foreign key constraints”? The  
> former is certainly needed by ActiveRecord/Rails for any non trivial  
> relationship between 2 tables. Leaving out foreign keys is not even an  
> option when working with relational entities.
> 
> Perhaps you meant foreign key constraints? Currently, their use  
> requires that you know vendor specific details of your db (which is not  
> usually a concern for most). Anyways, in my opinion, you are right in  
> that you can usually omit them until your DBA comes back from vacation  
> or you wake up in a pool of sweat knowing that you could have done more  
> to protect the integrity of your data.

If you want some level of integrity but don’t want to stick the  
constraints in the DB, you could use this:

[http://blog.hasmanythrough.com/2007/7/14/validate-your-existence](http://blog.hasmanythrough.com/2007/7/14/validate-your-existence)

It does something pretty close to fk constraints using Rails  
validations.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
