# Composite Primary Keys

**URL:** <https://www.ruby-forum.com/t/composite-primary-keys/88134>\
**Category:** Rails\
**Created:** [March 8, 2007, 12:44pm UTC](https://www.ruby-forum.com/t/composite-primary-keys/88134 "2007-03-08T12:44:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [March 8, 2007, 12:44pm UTC](https://www.ruby-forum.com/t/composite-primary-keys/88134/1 "2007-03-08T12:44:37Z")

</div>

Have been looking into using rails but am finding the “Composite  
Primary Keys are Bad” thing a tad disturbing (to put it mildly). I  
have been working with RDBMS systems for over fifteen years and this  
is the first time I have herd the idea questioned. I have cone across  
using surrogate keys which mirror the composite key for performance  
reasons in VERY big databases but I have never herd the idea  
questioned from a logical database design point of view. Apart from  
it being easier in rails to use non composite keys has anyone got any  
other good reasons not to use them. As far as I am concerned they are  
fundamental to RDBMS.

## Regards, Ben

Ben E. - Brussels, Belgium & Bristol, UK  
If you have a problem emailing me use  
[http://www.gurtlush.org.uk/profiles.php?uid=4](http://www.gurtlush.org.uk/profiles.php?uid=4)  
(email address this email is sent from may be defunct)

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [March 8, 2007, 1:42pm UTC](https://www.ruby-forum.com/t/composite-primary-keys/88134/2 "2007-03-08T13:42:54Z")

</div>

Ben E. wrote:

> Have been looking into using rails but am finding the “Composite  
> Primary Keys are Bad” thing a tad disturbing (to put it mildly). I  
> have been working with RDBMS systems for over fifteen years and this  
> is the first time I have herd the idea questioned. I have cone across  
> using surrogate keys which mirror the composite key for performance  
> reasons in VERY big databases but I have never herd the idea  
> questioned from a logical database design point of view. Apart from  
> it being easier in rails to use non composite keys has anyone got any  
> other good reasons not to use them. As far as I am concerned they are  
> fundamental to RDBMS.

They are fundamental to RDBMSes. The problem is that they don’t map so  
well from the relational world to the object world so ORMs have to go  
through convolutions to handle them. That’s why if you are creating a  
“green field” application the recommendation is to use a single column  
as the unique key.

This problem is not unique to ActiveRecord. If you look at something  
Hibernate, it does support composite primary keys but you have to define  
your own “equality” method, even after you specify what columns make up  
the composite key.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [March 8, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/composite-primary-keys/88134/3 "2007-03-08T16:46:23Z")

</div>

Ben E. wrote:

> Have been looking into using rails but am finding the “Composite  
> Primary Keys are Bad” thing a tad disturbing (to put it mildly). I  
> have been working with RDBMS systems for over fifteen years and this  
> is the first time I have herd the idea questioned. I have cone across  
> using surrogate keys which mirror the composite key for performance  
> reasons in VERY big databases but I have never herd the idea  
> questioned from a logical database design point of view. Apart from  
> it being easier in rails to use non composite keys has anyone got any  
> other good reasons not to use them. As far as I am concerned they are  
> fundamental to RDBMS.

Ben,  
you can use composite primary keys in Rails. See  
[http://compositekeys.rubyforge.org/](http://compositekeys.rubyforge.org/)

–  
Sava C.
