# Using External Databases that Don't Have ID Columns

**URL:** <https://www.ruby-forum.com/t/using-external-databases-that-dont-have-id-columns/93259>\
**Category:** Rails\
**Created:** [April 30, 2007, 5:46am UTC](https://www.ruby-forum.com/t/using-external-databases-that-dont-have-id-columns/93259 "2007-04-30T05:46:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![willclarkdesigns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/85f322/32.png) [@willclarkdesigns](https://www.ruby-forum.com/u/willclarkdesigns)\
**Post date:** [April 30, 2007, 5:46am UTC](https://www.ruby-forum.com/t/using-external-databases-that-dont-have-id-columns/93259/1 "2007-04-30T05:46:38Z")

</div>

I have to connect to an external database which I don’t have control of.  
They were not setup with a Rails application in mind. I can extract  
information easily enough, but creating associations between the models  
is my problem. I am not certain how exactly to let Rails know what the  
primary key column is.

I have two tables, properties and photos. I would like to create an  
association like this:

Class Property  
has\_many :photos, :class\_name =\> “Photo”, :foreign\_key =\> “propNum”  
end

Class Photo  
belongs\_to :property, :class\_name =\> “Property”, :foreign\_key =\>  
“propNum”  
end

The properties table uses ‘propNum’ instead of ID. The photos table uses  
photoNum instead of ID, and associates to the properties table using a  
propNum column versus property\_id.

I am not certain how exactly to specify what the primary column name is  
so Rails can understand the association. Any help would be great.

---

<div class="post-metadata">

**Author:** ![willclarkdesigns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/85f322/32.png) [@willclarkdesigns](https://www.ruby-forum.com/u/willclarkdesigns)\
**Post date:** [April 30, 2007, 10:35am UTC](https://www.ruby-forum.com/t/using-external-databases-that-dont-have-id-columns/93259/2 "2007-04-30T10:35:17Z")

</div>

Hi!  
Following code should set the primary key to propNum.  
class Property  
set\_primary\_key ‘propNum’  
end  
Hope this helps,

Thanks and regards,  
Swanand

---

<div class="post-metadata">

**Author:** ![willclarkdesigns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/85f322/32.png) [@willclarkdesigns](https://www.ruby-forum.com/u/willclarkdesigns)\
**Post date:** [April 30, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/using-external-databases-that-dont-have-id-columns/93259/3 "2007-04-30T18:28:04Z")

</div>

I knew it would be something really easy! Thanks Swanand!
