# Is there have any way can build one-to-many relationship without primary key

**URL:** <https://www.ruby-forum.com/t/is-there-have-any-way-can-build-one-to-many-relationship-without-primary-key/145943>\
**Category:** Rails\
**Created:** [August 15, 2008, 10:58am UTC](https://www.ruby-forum.com/t/is-there-have-any-way-can-build-one-to-many-relationship-without-primary-key/145943 "2008-08-15T10:58:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 15, 2008, 10:58am UTC](https://www.ruby-forum.com/t/is-there-have-any-way-can-build-one-to-many-relationship-without-primary-key/145943/1 "2008-08-15T10:58:54Z")

</div>

I have two models,Book and Picture,one book can have many pictures.  
In database,I contact them by uuid column which is not a primary  
key,roughly like this:

Books  
Pictures  
|--------------------------------- |-------------1:\*(via  
uuid)----------|------------------------------------|  
|id | title | isbn |…|uuid  
| |id|create\_at|rating|…|

| uuid |
| --- |

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 15, 2008, 11:37am UTC](https://www.ruby-forum.com/t/is-there-have-any-way-can-build-one-to-many-relationship-without-primary-key/145943/2 "2008-08-15T11:37:38Z")

</div>

you can use :foreign\_key like  
has\_many :pictures, ::foreign\_key =\> “uuid”  
or if that doesn’t work you could use the :finder\_sql option  
and define you own sql to find the related records.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 15, 2008, 6:38pm UTC](https://www.ruby-forum.com/t/is-there-have-any-way-can-build-one-to-many-relationship-without-primary-key/145943/3 "2008-08-15T18:38:45Z")

</div>

> uuid|  
> Yes, you can…

I’m doing some work with occupational data right now and everything  
has an onet\_soc\_code which is not the primary key. It’s not  
documented, but you can do this (primary\_key tells the model to use  
that column as the local side for looking things up instead of the  
tables real primary key).

Occupation:  
has\_many :aliases,  
:class\_name =\> ‘OccupationAlias’,  
:primary\_key =\> :onet\_soc\_code,  
:foreign\_key =\> :onet\_soc\_code,  
:order =\> ‘title’

Going the other way requires a patch to Rails, but lets you do this:

OccupationAlias:  
belongs\_to :occupation,  
:primary\_key =\> :onet\_soc\_code,  
:foreign\_key =\> :onet\_soc\_code

The patch is here:

[http://rails.lighthouseapp.com/projects/8994/tickets/765-primary\_key-option-for-belongs\_to](http://rails.lighthouseapp.com/projects/8994/tickets/765-primary_key-option-for-belongs_to)

-philip
