# Rails with composite primary keys and string keys

**URL:** <https://www.ruby-forum.com/t/rails-with-composite-primary-keys-and-string-keys/170770>\
**Category:** Rails\
**Created:** [June 22, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/rails-with-composite-primary-keys-and-string-keys/170770 "2009-06-22T18:17:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 22, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/rails-with-composite-primary-keys-and-string-keys/170770/1 "2009-06-22T18:17:40Z")

</div>

The guy who is designing our database seems to have specified some  
keys that are strings as well as some composite keys which are part  
integer and part string.

I guess I could support that in rails by using find\_by\_sql() with no  
problem if I had to ?

I will mention to him that it may be extra work etc.

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 22, 2009, 6:50pm UTC](https://www.ruby-forum.com/t/rails-with-composite-primary-keys-and-string-keys/170770/2 "2009-06-22T18:50:35Z")

</div>

Jedrin wrote:

> The guy who is designing our database seems to have specified some  
> keys that are strings as well as some composite keys which are part  
> integer and part string.
> 
> I guess I could support that in rails by using find\_by\_sql() with no  
> problem if I had to ?

You could do even better. There’s a Rails plugin (don’t remember the  
name, but a Web search should find it) that makes Rails work  
transparently with composite keys.

In any case, find\_by\_sql will probably not be necessary, since you can  
do find\_by\_key\_field\_1\_and\_key\_field\_2 or use :conditions.

> I will mention to him that it may be extra work etc.

Tell him that Rails works better with a surrogate key…

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 22, 2009, 9:04pm UTC](https://www.ruby-forum.com/t/rails-with-composite-primary-keys-and-string-keys/170770/3 "2009-06-22T21:04:32Z")

</div>

On Monday 22 June 2009, Marnen Laibow-Koser wrote:

> transparently with composite keys.  
> [http://compositekeys.rubyforge.org/](http://compositekeys.rubyforge.org/)

I’ve referred people to it in the past, but I’ve never used it myself.  
I’d love to hear that CPK works without a hitch with all the Rails  
versions and plugins you care to throw at it, however, I fear that this  
is not the case.

The changes CPK needs to make all over Rails are rather intrusive and  
have to be adapted to the evolving code base. I’d expect a lot of code  
out there, plugins specifically, to tacitly assume that ids are plain  
numbers or can converted to numbers.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![Jedrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jedrin](https://www.ruby-forum.com/u/Jedrin)\
**Post date:** [June 22, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/rails-with-composite-primary-keys-and-string-keys/170770/4 "2009-06-22T21:55:31Z")

</div>

Michael S. wrote:

> On Monday 22 June 2009, Marnen Laibow-Koser wrote:
> 
> > transparently with composite keys.  
> > [http://compositekeys.rubyforge.org/](http://compositekeys.rubyforge.org/)
> 
> I’ve referred people to it in the past, but I’ve never used it myself.  
> I’d love to hear that CPK works without a hitch with all the Rails  
> versions and plugins you care to throw at it, however, I fear that this  
> is not the case.

It was the case for me, but it’s been quite a while since I actually  
used it. I was very impressed by how effortless and transparent the  
whole thing was.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
