# Foreign key constraints on polymorphic relationships

**URL:** <https://www.ruby-forum.com/t/foreign-key-constraints-on-polymorphic-relationships/145757>\
**Category:** Rails\
**Created:** [August 13, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/foreign-key-constraints-on-polymorphic-relationships/145757 "2008-08-13T18:35:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 13, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/foreign-key-constraints-on-polymorphic-relationships/145757/1 "2008-08-13T18:35:06Z")

</div>

I am setting up foreign key constraints in my database and wasn’t sure  
if it was possible to set them up on polymorphic relationships. Any help  
is greatly appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 13, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/foreign-key-constraints-on-polymorphic-relationships/145757/2 "2008-08-13T19:18:31Z")

</div>

Hi Ben,

> I am setting up foreign key constraints in my database and wasn’t sure  
> if it was possible to set them up on polymorphic relationships. Any help  
> is greatly appreciated. Thanks.

This isn’t possible by default. A rather complex workaround is to use  
stored procedures that fire on insert and update triggers, but I’d  
advise against it because it’s far from the Rails idiom.

–  
Roderick van Domburg

> **[De domeinnaam Nedforce.nl is te koop.](http://www.nedforce.nl)**
>
> Deze mooie domeinnaam kan al over een paar minuten van jou zijn. Betalen gaat eenvoudig en vertrouwd via iDEAL. Direct na de betaling ontvang je de verhuiscode per e-mail en kun je aan de slag.
