# Turning off ID's auto\_increment

**URL:** <https://www.ruby-forum.com/t/turning-off-ids-auto-increment/76874>\
**Category:** Rails\
**Created:** [November 21, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/turning-off-ids-auto-increment/76874 "2006-11-21T22:01:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 21, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/turning-off-ids-auto-increment/76874/1 "2006-11-21T22:01:00Z")

</div>

For one of my objects it’s ‘id’ will be taken from another system so I  
don’t want it auto\_incrementing but I want to just assign a value  
myself to it? How can I create an id field while turning off  
auto\_increment for it within a migration?

Thanks my friends :-).

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 21, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/turning-off-ids-auto-increment/76874/2 "2006-11-21T22:41:59Z")

</div>

John K. wrote:

> For one of my objects it’s ‘id’ will be taken from another system so I  
> don’t want it auto\_incrementing but I want to just assign a value  
> myself to it? How can I create an id field while turning off  
> auto\_increment for it within a migration?

create\_table(:table\_name) **:id=\>false** do |t|  
t.column :id, :int  
end

The “:id=\>false” is used all the time in associative(link) tables and  
should probably work in your case.

You can also probably leverage before\_create() to retrieve the id from  
your other table and then do attributes[:id] =

I hope this helps…

ilan

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 21, 2006, 10:43pm UTC](https://www.ruby-forum.com/t/turning-off-ids-auto-increment/76874/3 "2006-11-21T22:43:14Z")

</div>

Ilan B. wrote:

> create\_table(:table\_name) **:id=\>false** do |t|  
> t.column :id, :int  
> end

Ouch, please ignore the **tags, I forgot where I was posting… 🙂**

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 22, 2006, 12:03am UTC](https://www.ruby-forum.com/t/turning-off-ids-auto-increment/76874/4 "2006-11-22T00:03:22Z")

</div>

It works and it does not work :-).

For some reason rails still does not like me assigning a value to id  
even if it is not a primary key. How can I resolve this?

On 11/21/06, Ilan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [November 22, 2006, 4:41am UTC](https://www.ruby-forum.com/t/turning-off-ids-auto-increment/76874/5 "2006-11-22T04:41:50Z")

</div>

John –

You didn’t say what type of database you are using, but the place you  
should  
look is on ActiveRecord::ConnectionAdapters::AbstractAdapter –  
prefetch\_primary\_key?() and next\_sequence\_value(). You should probably  
look  
at subclassing this class, and returning false from  
prefetch\_primary\_key?.  
That will cause next\_sequence\_value to be called, and you can do what  
you  
need to there. The return from this function is assigned to the id of  
the  
ActiveRecord instance.

Take a look at the firebird or openbase adapters for examples.
