To read all comments associated with this story, please click here.
You only need compound primary keys if you want to use a legacy database.
If you are designing a database for a Rails app from scratch you are still expected to have a column called 'id' that is the primary key in each table.
Now that the ids of rows are used to build up the RESTful urls, it has become harder to not use this convention. For example, if you have a url of /posts/1/comments/3 the '1' and the '3' are the primary keys of the post and the comment. If they were compound primary keys, you would probably end up with a pretty messy looking url. As well as needing to special case the models for compound primary keys, you would also need to special case the config/routes.rb file that generates the urls.
I've always felt slightly off about creating an arbitrary unique value unrelated to the actual context of the data when there is already available a way of uniquely identifying rows that only uses actual system data.
Also, by default most DBs create an index on the primary key, which is a bit meaningless with a created single key when most/all data access will still be by what would otherwise be the compound primary key. Just means you have to create an additional index, i realize, but there ya go.
So no real technical show stoppers, just a couple of things that kinda run against the grain for me anyway...
Is it possible to have compound primary keys in Rails 2.0? I remember reading that the Rails developers explicitly rejected compound primary keys in Rails 1.0 because they thought there was no legitimate reason to have them
The only reason to use them is for legacy database reasons, but even then, you'd be better off refactoring and normalising your database design - although, as borker says, I'm not keen on having peripheral stuff in my databases that isn't related to the actual data.
Edited 2007-12-07 23:25
An interesting link with a discussion on composite primary keys:
http://weblogs.sqlteam.com/jeffs/archive/2007/08/23/composite_prima...
I've read it all and now am really asking myself how ignorant can the Rails developers get?
Summary: Rails encourages sloppy db design!






Member since:
2005-07-09
Is it possible to have compound primary keys in Rails 2.0? I remember reading that the Rails developers explicitly rejected compound primary keys in Rails 1.0 because they thought there was no legitimate reason to have them (in their eyes) and that any legacy system that used them should be rewritten "the right way".
It's possible to create a Rails-type system with compound primary keys and user generated sequences (see http://framework.zend.com/manual/en/zend.db.table.html ) which might not have been obvious at the time the Rail developers made that statement was made (since Rails was a pioneer). So I'm wondering if they've changed their minds and included this functionality in 2.0?