# Using database constraints

**URL:** <https://www.ruby-forum.com/t/using-database-constraints/195695>\
**Category:** Rails\
**Created:** [September 28, 2010, 10:53am UTC](https://www.ruby-forum.com/t/using-database-constraints/195695 "2010-09-28T10:53:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [September 28, 2010, 10:53am UTC](https://www.ruby-forum.com/t/using-database-constraints/195695/1 "2010-09-28T10:53:57Z")

</div>

Do you think it’s necessary to use database constraints like foreign  
keys or it is safe to use only rails constraints like has\_many and so  
on.

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [September 28, 2010, 5:01pm UTC](https://www.ruby-forum.com/t/using-database-constraints/195695/2 "2010-09-28T17:01:30Z")

</div>

Msan M. wrote:

> Do you think it’s necessary to use database constraints like foreign  
> keys or it is safe to use only rails constraints like has\_many and so  
> on.

It’s absolutely necessary to use foreign key constraints – has\_many and  
things like that aren’t really constraints, and anyway, they only come  
into play if the DB is modified through the ActiveRecord interface. To  
keep your data safe, you _must_ use foreign key constraints. Use the  
Foreigner plugin to manage these constraints easily from migrations.

Search the list archives for more info.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [September 28, 2010, 10:53pm UTC](https://www.ruby-forum.com/t/using-database-constraints/195695/3 "2010-09-28T22:53:36Z")

</div>

There is also foreigner gem, version 0.9.0.  
Is it better to use the plugin or the gem?

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [September 29, 2010, 4:18am UTC](https://www.ruby-forum.com/t/using-database-constraints/195695/4 "2010-09-29T04:18:26Z")

</div>

Please quote when replying

Msan M. wrote:

> There is also foreigner gem, version 0.9.0.  
> Is it better to use the plugin or the gem?

Most likely the gem. When I said “plugin” I meant “plugin or  
plugin-type gem”.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
