# How to use set\_primary\_key?

**URL:** <https://www.ruby-forum.com/t/how-to-use-set-primary-key/156219>\
**Category:** Rails\
**Created:** [December 22, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/how-to-use-set-primary-key/156219 "2008-12-22T22:52:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [December 22, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/how-to-use-set-primary-key/156219/1 "2008-12-22T22:52:02Z")

</div>

We are trying to use set\_primary\_key to cause an existing rails db to  
use a different primary key. This works find for accessing existing  
data, but gives an error when we try to add a new record, as it seems  
to still be accessing the record by the default id field at that  
point. Are there other changes we need to make to get an alternate  
primary key working?

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [December 22, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/how-to-use-set-primary-key/156219/2 "2008-12-22T23:25:27Z")

</div>

I have used the set\_primary\_key with some 3rd party propriety  
databases with no issues you describe.

Could you give us some more information (table structure and such) to  
be able to help you adequately.

On Dec 22, 10:51 pm, Kenneth McDonald

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [December 22, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/how-to-use-set-primary-key/156219/3 "2008-12-22T23:34:14Z")

</div>

I think the problem is likely that the “old” primary key in the  
database is still the “id” field that rails generates and uses by  
default, and we can’t change that since (for the time being, our needs  
being modest) we’re using sqlite. We want to use a field called “rmr”,  
basically an identification number, as the primary key, but this  
became obvious only after we’d created and started populating the  
database.

Does Rails interrogate the database to find out which column is the  
primary key?

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [December 24, 2008, 5:11am UTC](https://www.ruby-forum.com/t/how-to-use-set-primary-key/156219/4 "2008-12-24T05:11:12Z")

</div>

I think I read somewhere that when creating a new record you still  
have to reference the field as ‘id’ (id=value) or it will not work.  
It’s worth the try.

Pepe

On Dec 22, 5:33 pm, Kenneth McDonald
