# Anyone find workaround for composite primary keys in AR?

**URL:** <https://www.ruby-forum.com/t/anyone-find-workaround-for-composite-primary-keys-in-ar/49363>\
**Category:** Rails\
**Created:** [December 2, 2005, 10:34pm UTC](https://www.ruby-forum.com/t/anyone-find-workaround-for-composite-primary-keys-in-ar/49363 "2005-12-02T22:34:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodney.clang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/858c86/32.png) [@rodney.clang](https://www.ruby-forum.com/u/rodney.clang)\
**Post date:** [December 2, 2005, 10:34pm UTC](https://www.ruby-forum.com/t/anyone-find-workaround-for-composite-primary-keys-in-ar/49363/1 "2005-12-02T22:34:45Z")

</div>

These old legacy tables are going to get the best of me yet. I have 3  
fields comprising the primary key in some instances. Unfortunately can’t  
add an ID auto increment field.

I was wondering if anyone did anything tricky with views, replication or  
fancy Ruby code to adress this big legacy data need ?  
-r

---

<div class="post-metadata">

**Author:** ![rodney.clang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/858c86/32.png) [@rodney.clang](https://www.ruby-forum.com/u/rodney.clang)\
**Post date:** [December 7, 2005, 3:24am UTC](https://www.ruby-forum.com/t/anyone-find-workaround-for-composite-primary-keys-in-ar/49363/2 "2005-12-07T03:24:09Z")

</div>

On Dec 3, 2005, at 6:32 , Rodney Clang wrote:

> These old legacy tables are going to get the best of me yet. I have  
> 3 fields comprising the primary key in some instances.  
> Unfortunately can’t add an ID auto increment field.
> 
> I was wondering if anyone did anything tricky with views,  
> replication or fancy Ruby code to adress this big legacy data need ?

I’m sorry, I haven’t been following this thread, but had an idea that  
might be workable if you’re using PostgreSQL (or another dbms that  
supports views and rules).

Create a table with four columns, one of which is serial and will be  
your Rails-friendly pkey, e.g.,

create table pk\_join\_table (  
id serial primary key  
, compound\_pkey\_part1 footype not null  
, compound\_pkey\_part2 bartype not null  
, compound\_pkey\_part3 baztype not null  
, unique (compound\_pkey\_part1,compound\_pkey\_part2,compound\_pkey\_part3)  
, foreign key  
(compound\_pkey\_part1,compound\_pkey\_part2,compound\_pkey\_part3)  
references legacy\_table ( compound\_pkey\_part1, compound\_pkey\_part2  
, compound\_pkey\_part3)  
);

Then create a view:

create view new\_rails\_friendly\_table as  
select \*  
from pk\_join\_table  
join legacy\_table using (compound\_pkey\_part1, compound\_pkey\_part2  
, compound\_pkey\_part3)  
;

Then you could create the appropriate insert, update, and delete  
rules in PostgreSQL to make sure things get properly placed. Map  
ActiveRecord to the view, not the pk\_join\_table or legacy\_table. (I  
believe it’s possible, but I don’t have much experience writing rules.)

I’m not sure how Rails will handle pkey generation though. The folks  
putting together the UUID (or is it GUID?) work for Rails might know  
how to get around that.

Hope this helps a bit.

Michael G.  
grzm myrealbox com
