Forum: Ruby on Rails Composite primary key

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Hugo Wetterberg (Guest)
on 2006-01-11 10:31
(Received via mailing list)
Hi All,
How do I specify a composite primary key for a table in ActiveRecord?
For example the table Recipe which has the primary key [cookie id,
number] where cookie id is also a foreign key referencing Cookie.

Also: Thanks to Nic and Adam for helping with the Cookie -> Cooky


CREATE* *TABLE* `Raw_Materials`*
       id                      *INT* AUTO_INCREMENT*,*
       `name`          *VARCHAR**(*30*),*
       quantity                *INT**,*
       lastDelivery    DATETIME*,*
       *PRIMARY* *KEY* *(*id*)**

*CREATE* *TABLE* `Cookies`
       id                      *INT* AUTO_INCREMENT*,*
       `name`          *VARCHAR**(*30*),*
       *primary* *key* *(*id*)*

*CREATE* *TABLE* `Recipes`
       `version`               *INT**,*
       cookie_id               *INT**,*
       *PRIMARY* *KEY* *(*version*,* cookie_id*),*
       *FOREIGN* *KEY* *(*cookie_id*)* *REFERENCES* Cookies*(*id*)*

*CREATE* *TABLE* `Ingredients`
       `version`                       *INT**,*
       cookie_id                       *INT**,*
       raw_material_id *INT**,*
       quantity                        *INT**,*
       *PRIMARY* *KEY* *(*version*,* cookie_id*,* raw_material_id*),*
       *FOREIGN* *KEY* *(*version*,* cookie_id*)* *REFERENCES*
version*,* cookie_id*),*
       *FOREIGN* *KEY* *(*raw_material_id*)* *REFERENCES*
Chris H. (Guest)
on 2006-01-11 14:53
(Received via mailing list)
Rails doesn't support composite primary keys (at the moment).  if you're
doing new development, just use an 'id' column as your primary key.
This topic is locked and can not be replied to.