# Manually setting an auto-incremented primary key id

**URL:** <https://www.ruby-forum.com/t/manually-setting-an-auto-incremented-primary-key-id/160199>\
**Category:** Rails\
**Created:** [February 12, 2009, 10:22pm UTC](https://www.ruby-forum.com/t/manually-setting-an-auto-incremented-primary-key-id/160199 "2009-02-12T22:22:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamie\_f](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47774/32.png) [@jamie\_f](https://www.ruby-forum.com/u/jamie_f)\
**Post date:** [February 12, 2009, 10:22pm UTC](https://www.ruby-forum.com/t/manually-setting-an-auto-incremented-primary-key-id/160199/1 "2009-02-12T22:22:24Z")

</div>

I want to create a record with a manually-set custom ID, for instance:

Foo.create(:id =\> 8000, :name =\>“bar”)

But when I try to do that, Rails ignores the id I pass and continues  
to auto-increment the id in the table. So in other words the console  
output is along the lines of:

> > Foo.find(:last)  
> > =\> #\<Foo id: 52, name: “foo”\>
> 
> > Foo.create(:id =\> 8000, :name =\>“bar”)  
> > =\> #\<Foo id: 53, name: “bar”\>

What’s the best way around this? (By the way, after I insert this  
record with the custom ID, I _do_ want it to revert back to auto-  
incrementing the id.)

---

<div class="post-metadata">

**Author:** ![jamie\_f](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47774/32.png) [@jamie\_f](https://www.ruby-forum.com/u/jamie_f)\
**Post date:** [February 13, 2009, 12:36am UTC](https://www.ruby-forum.com/t/manually-setting-an-auto-incremented-primary-key-id/160199/2 "2009-02-13T00:36:17Z")

</div>

On Feb 12, 2009, at 4:21 PM, Jamie F. wrote:

> > > Foo.create(:id =\> 8000, :name =\>“bar”)  
> > > =\> #\<Foo id: 53, name: “bar”\>
> 
> What’s the best way around this? (By the way, after I insert this  
> record with the custom ID, I _do_ want it to revert back to auto-  
> incrementing the id.)

Foo.create(:name =\> ‘bar’) do |foo|  
foo.id = 8000  
end

One caveat however that bit me last week doing something similar when  
initializing some records in a new table. PostgreSQL has a sequence  
created to support the auto-incrementing primary key (id) and I was  
getting unique index errors when new (or editted!) records were being  
saved and the sequence value was already a different record in the  
table.

(My solution was to manually next values off each of the two sequences  
until it would return a value that wasn’t already a key.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jamie\_f](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47774/32.png) [@jamie\_f](https://www.ruby-forum.com/u/jamie_f)\
**Post date:** [February 13, 2009, 4:08am UTC](https://www.ruby-forum.com/t/manually-setting-an-auto-incremented-primary-key-id/160199/3 "2009-02-13T04:08:36Z")

</div>

On Feb 12, 6:35 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > output is along the lines of:  
> > Foo.create(:name =\> ‘bar’) do |foo|  
> > (My solution was to manually next values off each of the two sequences  
> > until it would return a value that wasn’t already a key.)
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

Thanks that worked like a charm.
