# Linking database when primary key is not id

**URL:** <https://www.ruby-forum.com/t/linking-database-when-primary-key-is-not-id/75507>\
**Category:** Rails\
**Created:** [November 10, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/linking-database-when-primary-key-is-not-id/75507 "2006-11-10T14:54:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://www.ruby-forum.com/u/Rajkumar_S)\
**Post date:** [November 10, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/linking-database-when-primary-key-is-not-id/75507/1 "2006-11-10T14:54:18Z")

</div>

Hi,

I have 2 tables that I had created with migrations, but these are  
based on existing database. So the automatically created id column is  
not used. These tables have the following structure.

Table calls  
±------------±-------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±------------±-------------±-----±----±--------±---------------+  
| id | int(11) | | PRI | NULL | auto\_increment |  
| src | varchar(80) | YES | | NULL | |  
| dst | varchar(80) | YES | | NULL | |  
| ivr\_id | int(11) | YES | | NULL | |  
±------------±-------------±-----±----±--------±---------------+

and Table clients  
±----------±--------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±----------±--------±-----±----±--------±---------------+  
| id | int(11) | | PRI | NULL | auto\_increment |  
| ivr\_id | int(11) | YES | | NULL | |  
| is\_online | char(1) | YES | | NULL | |  
±----------±--------±-----±----±--------±---------------+

The ivr\_id in calls table is the foreign key referring to ivr\_id in  
clients table. How can I specify this relation in RoR model so that  
rails “understand” which columns to link to when I specify the  
has\_many and belongs\_to in respective models.

Thanks for reading,  
raj

---

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://www.ruby-forum.com/u/Rajkumar_S)\
**Post date:** [November 10, 2006, 3:50pm UTC](https://www.ruby-forum.com/t/linking-database-when-primary-key-is-not-id/75507/2 "2006-11-10T15:50:53Z")

</div>

Rajkumar S wrote:

> Hi,
> 
> I have 2 tables that I had created with migrations, but these are  
> based on existing database. So the automatically created id column is  
> not used. These tables have the following structure.
> 
> Table calls  
> ±------------±-------------±-----±----±--------±---------------+  
> | Field | Type | Null | Key | Default | Extra |  
> ±------------±-------------±-----±----±--------±---------------+  
> | id | int(11) | | PRI | NULL | auto\_increment |  
> | src | varchar(80) | YES | | NULL | |  
> | dst | varchar(80) | YES | | NULL | |  
> | ivr\_id | int(11) | YES | | NULL | |  
> ±------------±-------------±-----±----±--------±---------------+
> 
> and Table clients  
> ±----------±--------±-----±----±--------±---------------+  
> | Field | Type | Null | Key | Default | Extra |  
> ±----------±--------±-----±----±--------±---------------+  
> | id | int(11) | | PRI | NULL | auto\_increment |  
> | ivr\_id | int(11) | YES | | NULL | |  
> | is\_online | char(1) | YES | | NULL | |  
> ±----------±--------±-----±----±--------±---------------+
> 
> The ivr\_id in calls table is the foreign key referring to ivr\_id in  
> clients table. How can I specify this relation in RoR model so that  
> rails “understand” which columns to link to when I specify the  
> has\_many and belongs\_to in respective models.
> 
> Thanks for reading,  
> raj

Use the :foreign\_key parm in your associations. See the API has\_many  
documentation:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

and belongs\_to documentation:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000532)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

c.

---

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://www.ruby-forum.com/u/Rajkumar_S)\
**Post date:** [November 10, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/linking-database-when-primary-key-is-not-id/75507/3 "2006-11-10T21:02:58Z")

</div>

Rajkumar S wrote:

> On 11/10/06, Cayce B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Use the :foreign\_key parm in your associations. See the API has\_many  
> > documentation:
> 
> Thanks a lot for your helpful answer.
> 
> raj

Raj

I would recommend making a few changes to your DB schema if the tables  
you posted are actually the ones you are using for your application. The  
structure of the tables you posted opens you up to data integrity issues  
and unpredictable application behaviour.

If you want to use clients.ivr\_id as a target for a foreign key  
reference, you should define a unique constraint on it in your DB. In  
order to define the foreign key constraint “calls.ivr\_id references  
clients.ivr\_id” on your database (which you should do), your DB will  
require that the target (clients.ivr\_id) have a unique constraint on it.  
Some databases will actually insist that it be the primary key of the  
referenced table (I believe MySQL does this).

Ideally, since you mentioned that you won’t be using the “id” column in  
the clients table, you should delete it and define ivr\_id as the primary  
key for the clients table.

Cheers.

- Bryan

---

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://www.ruby-forum.com/u/Rajkumar_S)\
**Post date:** [November 11, 2006, 11:10am UTC](https://www.ruby-forum.com/t/linking-database-when-primary-key-is-not-id/75507/4 "2006-11-11T11:10:58Z")

</div>

On 11/11/06, Bryan E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would recommend making a few changes to your DB schema if the tables  
> you posted are actually the ones you are using for your application. The  
> structure of the tables you posted opens you up to data integrity issues  
> and unpredictable application behaviour.

Thanks for the heads up. The clients.ivr\_id is supposed to be a unique  
field so I should have a unique constraint on it.

> Ideally, since you mentioned that you won’t be using the “id” column in  
> the clients table, you should delete it and define ivr\_id as the primary  
> key for the clients table.

Is there a “rails” way to do this, ie via a migration. as far as I  
know the id is created automatically, so there must be some way to  
disable it and assign another column as the key field.

raj

raj

---

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://www.ruby-forum.com/u/Rajkumar_S)\
**Post date:** [November 10, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/linking-database-when-primary-key-is-not-id/75507/5 "2006-11-10T20:01:13Z")

</div>

On 11/10/06, Cayce B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Use the :foreign\_key parm in your associations. See the API has\_many  
> documentation:

Thanks a lot for your helpful answer.

raj
