[Rails] Re: multiple keys table

Derrick Spell derrickspell at cdmplus.com
Wed Mar 1 13:56:58 GMT 2006


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

> Anthony Green 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.
>

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 Spell


More information about the Rails mailing list