Turning off ID's auto_increment

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.
[email protected]

http://www.kopanas.com
http://www.cusec.net
http://www.soen.info

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

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… :slight_smile:

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. [email protected] wrote:


Posted via http://www.ruby-forum.com/.


John K.
[email protected]

http://www.kopanas.com
http://www.cusec.net
http://www.soen.info

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs