Forum: Ruby on Rails Composite primary key

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3a8361465969c6e088a6fd994f0924b4?d=identicon&s=25 Hugo Wetterberg (Guest)
on 2006-01-11 09: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,
version
number] where cookie id is also a foreign key referencing Cookie.

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

/Hugo

--SQL
*
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*
Recipes*(*
version*,* cookie_id*),*
       *FOREIGN* *KEY* *(*raw_material_id*)* *REFERENCES*
Raw_Materials*(*id
*)*
*);*
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-01-11 13: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.