JaTo
JaTo

Reputation: 2832

Rails association primary_key option

I understand that foreign keys are for specifying when you have a different column name (different than parent child's class name) on the child class. I know what primary keys and foreign keys are and have read the rails documentation on associations several times, but I can't figure out what the primary key option is for.

1) But what is the primary_key option for? How does it change the sql when an association is called?

2) In what instances would you need to specify the primary_key on association?

3) In what instances would you need to specify both the primary_key and foreign_key?

below is an example of specifying the foreign_key option on associations:

class User
has_many :texts, foreign_key: :owner_id

end

class Text
belongs_to :user, foreign_key: :owner_id

end


User Table
id| name |

Text Table
id| owner_id |name

Upvotes: 3

Views: 5215

Answers (3)

JaTo
JaTo

Reputation: 2832

Ok so I thought more about the SQL and figured it out. You use foreign_key option when your child has a different foreign_key name then your parent's classname_id BUT on your parent table, you are still using ID as your identifier.

user table
id|name|age

text table
id|random_id|conversations

select * from user where user.id = text.random_id

select * from text where text.random_id (foreign_key) = account.id (primary key)

On the other hand, you use primary_key with foreign_key when you don't want to use id at all to link the relationship.

user table
id|userable_id|name|age

text table
id|userable_ss_id|conversations

HERE: if you wanted to link the userable_ss_id to userable_id, you would include both primary_key and foreign_key options on both relationships.

class User 
has_many :texts, primary_key: :userable_ss_id, foreign_key: :userable_id
end

class Text 
belongs_to :user, primary_key: :userable_ss_id, foreign_key: :userable_id
end

Basic rule of thumb:

select * from text where text.(foreign_key) = account.(primary key)

Upvotes: 4

Taryn East
Taryn East

Reputation: 27757

Rails uses convention over configuration.

By convention, all database tables in rails have a primary-key of the id column.

If, for some odd reason (eg you've got a legacy database), your table uses a different primary key to id... you use the primary_key call to tell rails what it is.

By convention, all associations use a foreign-key of <model>_id for the foreign key.

If, for some reason, your association uses a different foreign-key to find the associated model - you'd use foreign_key to tell rails what it is.

Unlike primary_key, using foreign_key can be much more common. Especially when you have more than one association using the same table but with different association names.

Upvotes: 2

dre-hh
dre-hh

Reputation: 8044

primary key concerns the main table and foreign key the associated one

U 've used the foreign key correctly. If for isntance User had another primary key than :id u'd have to specify that either.

#User
 has_many :texts, primary_key: :uuid, foreign_key: owner_id

So you need this options, if you want to have another naming of the keys than rails conventions assume for the main and associated table respectivly

Upvotes: 2

Related Questions