Linked by Thom Holwerda on Fri 7th Dec 2007 19:44 UTC, submitted by Bill Davenport
General Development Ruby on Rails 2.0 has been released. "Rails 2.0 is finally finished after about a year in the making. This is a fantastic release that's absolutely stuffed with great new features, loads of fixes, and an incredible amount of polish. We've even taken a fair bit of cruft out to make the whole package more coherent and lean." On Zenbits, they give tips on how to install 2.0.
Thread beginning with comment 289447
To read all comments associated with this story, please click here.
Question: Compound primary keys in Rails?
by g2devi on Fri 7th Dec 2007 22:14 UTC
g2devi
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?

Richard Dale Member since:
2005-07-22

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.

Reply Parent Bookmark Score: 3

FooBarWidget Member since:
2005-11-11

It still doesn't.

But why do you need compound primary keys? What are its advantages over using a number? I've never felt the need to use compound primary keys.

Reply Parent Bookmark Score: 4

borker Member since:
2006-04-04

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

Reply Parent Bookmark Score: 4

segedunum Member since:
2005-07-06

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

Reply Parent Bookmark Score: 1

franzb Member since:
2007-12-08

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!

Reply Parent Bookmark Score: 1