# Multiple keys table

**URL:** <https://www.ruby-forum.com/t/multiple-keys-table/74739>\
**Category:** Rails\
**Created:** [February 28, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/multiple-keys-table/74739 "2006-02-28T15:26:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shiko](https://www.ruby-forum.com/u/shiko)\
**Post date:** [February 28, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/multiple-keys-table/74739/1 "2006-02-28T15:26:19Z")

</div>

probablly a newbie question:  
Rails does not support a table with multiple keys ???  
it seems that the ActiveRecord set\_primary\_key method can only set the  
column name… (hope i’m wrong there)

example:  
table A - P.K id, string name  
table B - P.K id, string name  
table C - P.K a\_id and b\_id, both are also foreign keys.

Thanks,  
Amir.

---

<div class="post-metadata">

**Author:** ![shiko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shiko](https://www.ruby-forum.com/u/shiko)\
**Post date:** [February 28, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/multiple-keys-table/74739/2 "2006-02-28T16:11:28Z")

</div>

amir lidor wrote:

> probablly a newbie question:  
> Rails does not support a table with multiple keys ???  
> it seems that the ActiveRecord set\_primary\_key method can only set the  
> column name… (hope i’m wrong there)

Sorry to be the carrier of bad news, but you are correct as far as I  
know.

---

<div class="post-metadata">

**Author:** ![shiko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shiko](https://www.ruby-forum.com/u/shiko)\
**Post date:** [February 28, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/multiple-keys-table/74739/3 "2006-02-28T16:19:32Z")

</div>

amir lidor wrote:

> probablly a newbie question:  
> Rails does not support a table with multiple keys ???  
> it seems that the ActiveRecord set\_primary\_key method can only set the  
> column name… (hope i’m wrong there)
> 
> example:  
> table A - P.K id, string name  
> table B - P.K id, string name  
> table C - P.K a\_id and b\_id, both are also foreign keys.

What is the relationship between tables A and B ?  
One to many, many to one, many to many ?  
I just wondered if your normalization was correct…  
\_tony

---

<div class="post-metadata">

**Author:** ![shiko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shiko](https://www.ruby-forum.com/u/shiko)\
**Post date:** [March 1, 2006, 7:57am UTC](https://www.ruby-forum.com/t/multiple-keys-table/74739/4 "2006-03-01T07:57:29Z")

</div>

Anthony G. wrote:

> amir lidor wrote:
> 
> > probablly a newbie question:  
> > Rails does not support a table with multiple keys ???  
> > it seems that the ActiveRecord set\_primary\_key method can only set the  
> > column name… (hope i’m wrong there)
> > 
> > example:  
> > table A - P.K id, string name  
> > table B - P.K id, string name  
> > table C - P.K a\_id and b\_id, both are also foreign keys.
> 
> What is the relationship between tables A and B ?  
> One to many, many to one, many to many ?  
> I just wondered if your normalization was correct…  
> \_tony

The relation is many to many.  
I talked with a friend though and he explained me that it’s really bad  
to use multiple-keys tables, and that if i assign another primary key  
column, even if there will be multiple identical (value-wise of the a\_id  
and b\_id columns) rows, RoR will ignore them when performing Joins.  
so i understand what should i do, I am just left with the question (out  
of curiosity) about Rails supporting multiple-keys tables…

Thanks,  
Amir.

---

<div class="post-metadata">

**Author:** ![shiko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@shiko](https://www.ruby-forum.com/u/shiko)\
**Post date:** [March 1, 2006, 2:58pm UTC](https://www.ruby-forum.com/t/multiple-keys-table/74739/5 "2006-03-01T14:58:06Z")

</div>

On Mar 1, 2006, at 1:57 AM, amir lidor wrote:

> > > table B - P.K id, string name  
> > > column, even if there will be multiple identical (value-wise of the  
> > > a\_id  
> > > and b\_id columns) rows, RoR will ignore them when performing Joins.  
> > > so i understand what should i do, I am just left with the question  
> > > (out  
> > > of curiosity) about Rails supporting multiple-keys tables…
> 
> Thanks,  
> Amir.

It sounds like you want a composite primary key, i.e. the combination  
of (a\_id, b\_id) form the primary key for table C.  
No, this is not supported in Rails. Such practice has fallen out of  
favor in recent years, and some database servers do not even support  
it. There is still much debate, but Rails has taken the position of  
rejecting composite primary keys.

-Derrick S.
