# Primary key?

**URL:** <https://www.ruby-forum.com/t/primary-key/133265>\
**Category:** Rails\
**Created:** [March 29, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/primary-key/133265 "2008-03-29T15:50:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 29, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/primary-key/133265/1 "2008-03-29T15:50:04Z")

</div>

can you use 2 pirmary keys.

the standard user\_id key exists but i would also like to use another  
key for my links. i am not sure how to implement this. also i don’t  
want any auto incrementing of this second primary key that i would  
like to use for my relationships.

if there is a good db, model, relationship tutorial out there i would  
love to know about it.

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 29, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/primary-key/133265/2 "2008-03-29T15:59:31Z")

</div>

are you mixing up “primary keys” and foreign keys" ?

Every Rails model has, by default, an auto-incrementing “id” column as  
primary key, and columns like “user\_id” are no primary keys, but  
foreign keys to link to related objects/rows in other tables. those  
are not auto-incrementing and not primary. and you can have as many od  
them as you want.

anyways, if your really need 2 primary keys in a model, try this:

[http://compositekeys.rubyforge.org/](http://compositekeys.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 29, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/primary-key/133265/3 "2008-03-29T17:45:21Z")

</div>

yes, i think i am mixing up primary, foreign

so do i declare  
belongs\_to :user foreign\_key “my\_id”

model (user), my\_id, name, country\_id  
model (movies), also has my\_id, movie\_id  
model (country), country details

i need to output the name of the person that has a given movie wihout  
having to use nested finds.

so i need output thru the movie model: name of user and then find  
details about the country as well.

> [http://compositekeys.rubyforge.org/](http://compositekeys.rubyforge.org/)

i did look at this earlier.

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 31, 2008, 6:41am UTC](https://www.ruby-forum.com/t/primary-key/133265/4 "2008-03-31T06:41:49Z")

</div>

It depends on the association type. Every table has an id on it  
automatically (hopefully).

say you have users and groups.

a group has many users

table name users, has a column called group\_id  
class User \< AR  
belongs\_to :group  
end

table name groups  
class Group \< AR  
has\_many :users  
end

if you wanted to spell out the foreign key, you’d put has\_many :users,  
foreign\_key :group\_id. The belongs\_to will be belongs\_to :group,  
foreign\_key :user\_id

Julian.

Learn about Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME)  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)
