Linking database when primary key is not id

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

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:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530

and belongs_to documentation:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000532

c.

Rajkumar S wrote:

On 11/10/06, Cayce B. [email protected] 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

On 11/11/06, Bryan E. [email protected] 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

On 11/10/06, Cayce B. [email protected] wrote:

Use the :foreign_key parm in your associations. See the API has_many
documentation:

Thanks a lot for your helpful answer.

raj

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs