1 h1. Composite Primary Keys
9 Ruby on Rails does not support composite primary keys. This free software is an extension
10 to the database layer of Rails - "ActiveRecords":http://wiki.rubyonrails.com/rails/pages/ActiveRecord - to support composite primary keys as transparently as possible.
12 Any Ruby script using ActiveRecords can use Composite Primary Keys with this library.
16 <pre syntax="ruby">sudo gem install composite_primary_keys</pre>
18 Rails: Add the following to the bottom of your <code>environment.rb</code> file
20 <pre syntax="ruby">require 'composite_primary_keys'</pre>
22 Ruby scripts: Add the following to the top of your script
24 <pre syntax="ruby">require 'rubygems'
25 require 'composite_primary_keys'</pre>
29 A model with composite primary keys would look like...
31 <pre syntax="ruby">class Membership < ActiveRecord::Base
32 # set_primary_keys *keys - turns on composite key functionality
33 set_primary_keys :user_id, :group_id
36 has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
39 A model associated with a composite key model would be defined like...
41 <pre syntax="ruby">class MembershipStatus < ActiveRecord::Base
42 belongs_to :membership, :foreign_key => [:user_id, :group_id]
45 That is, associations can include composite keys too. Nice.
47 h2. Demonstration of usage
49 Once you've created your models to specify composite primary keys (such as the Membership class) and associations (such as MembershipStatus#membership), you can uses them like any normal model with associations.
51 But first, lets check out our primary keys.
53 <pre syntax="ruby">MembershipStatus.primary_key # => "id" # normal single key
54 Membership.primary_key # => [:user_id, :group_id] # composite keys
55 Membership.primary_key.to_s # => "user_id,group_id"</pre>
57 Now we want to be able to find instances using the same syntax we always use for ActiveRecords...
59 <pre syntax="ruby">MembershipStatus.find(1) # single id returns single instance
60 => <MembershipStatus:0x392a8c8 @attributes={"id"=>"1", "status"=>"Active"}>
61 Membership.find(1,1) # composite ids returns single instance
62 => <Membership:0x39218b0 @attributes={"user_id"=>"1", "group_id"=>"1"}></pre>
64 Using "Ruby on Rails":http://www.rubyonrails.org? You'll want to your url_for helpers
65 to convert composite keys into strings and back again...
67 <pre syntax="ruby">Membership.find(:first).to_param # => "1,1"</pre>
69 And then use the string id within your controller to find the object again
71 <pre syntax="ruby">params[:id] # => '1,1'
72 Membership.find(params[:id])
73 => <Membership:0x3904288 @attributes={"user_id"=>"1", "group_id"=>"1"}></pre>
75 That is, an ActiveRecord supporting composite keys behaves transparently
76 throughout your application. Just like a normal ActiveRecord.
81 h3. Pass a list of composite ids to the <code>#find</code> method
83 <pre syntax="ruby">Membership.find [1,1], [2,1]
85 <Membership:0x394ade8 @attributes={"user_id"=>"1", "group_id"=>"1"}>,
86 <Membership:0x394ada0 @attributes={"user_id"=>"2", "group_id"=>"1"}>
89 Perform <code>#count</code> operations
91 <pre syntax="ruby">MembershipStatus.find(:first).memberships.count # => 1</pre>
98 I ran into one problem that I didn't see mentioned on "this list":http://groups.google.com/group/compositekeys -
99 and I didn't see any information about what I should do to address it in the
100 documentation (might have missed it).
102 The problem was that the urls being generated for a 'show' action (for
103 example) had a syntax like:
105 <pre>/controller/show/123000,Bu70</pre>
107 for a two-field composite PK. The default routing would not match that,
108 so after working out how to do the routing I added:
110 <pre syntax="ruby">map.connect ':controller/:action/:id', :id => /\w+(,\w+)*/</pre>
112 to my <code>route.rb</code> file.
121 A suite of unit tests have been run on the following databases supported by ActiveRecord:
123 |_.Database|_.Test Success|_.User feedback|
124 |mysql |<span class=success>YES</span>|<span class=success>YES</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Mysql+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Mysql+is+failing)|
125 |sqlite3 |<span class=success>YES</span>|<span class=success>YES</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Sqlite3+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Sqlite3+is+failing)|
126 |postgresql|<span class=success>YES</span>|<span class=success>YES</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Postgresql+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Postgresql+is+failing)|
127 |oracle |<span class=success>YES</span>|<span class=success>YES</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Oracle+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Oracle+is+failing)|
128 |sqlserver |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+SQLServer)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=SQLServer+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=SQLServer+is+failing)|
129 |db2 |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+DB2)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=DB2+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=DB2+is+failing)|
130 |firebird |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Firebird)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Firebird+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Firebird+is+failing)|
131 |sybase |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Sybase)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Sybase+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Sybase+is+failing)|
132 |openbase |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Openbase)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Openbase+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Openbase+is+failing)|
133 |frontbase |<span class=unknown>???</span> ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Frontbase)|<span class=unknown>???</span> ("Yes!":mailto:compositekeys@googlegroups.com?subject=Frontbase+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Frontbase+is+failing)|
137 "http://www.drnicwilliams.com":http://www.drnicwilliams.com - for future announcements and
138 other stories and things.
142 "http://groups.google.com/group/compositekeys":http://groups.google.com/group/compositekeys
144 h2. How to submit patches
146 Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the Google Group above.
149 The source for this project is available via git. You can "browse and/or fork the source":http://github.com/drnic/composite_primary_keys/tree/master, or to clone the project locally:
151 <pre>git clone git://github.com/drnic/composite_primary_keys.git</pre>
155 This code is free to use under the terms of the MIT licence.
159 Comments are welcome. Send an email to "Dr Nic Williams":mailto:drnicwilliams@gmail.com.