Is it possible in Rails to use the same sequence for multiple ID''s?
In PostgreSQL, I have some tables that share a sequence because later
they''re joined together in a view where each record from the different
tables needs a unique ID.
Does Rails ever attempt to set ID''s itself by using
nextval(''table_id_seq'')
(bad for me), or does Rails let the DB do its work without caring about these
sequences (good for me)?
- Rowan
--
Morality is usually taught by the immoral.
> Does Rails ever attempt to set ID''s itself by using nextval(''table_id_seq'') > (bad for me), or does Rails let the DB do its work without caring about these > sequences (good for me)?ActiveRecord lets the database do its own thing regarding primary keys. I actually messed up my postgresql development database''s sequences yesterday, with all inserts failing until I wiped the database completely. -- rick http://techno-weenie.net
On Jul 6, 2005, at 6:01 PM, BigSmoke wrote:> Is it possible in Rails to use the same sequence for multiple ID''s? > In PostgreSQL, I have some tables that share a sequence because later > they''re joined together in a view where each record from the different > tables needs a unique ID. > > Does Rails ever attempt to set ID''s itself by using nextval > (''table_id_seq'') > (bad for me), or does Rails let the DB do its work without caring > about these > sequences (good for me)?Fixtures attempt to reset the sequence associated with a table''s primary key. As long as you have a sequence name #{table}_id_seq (even if you don''t use it), though, you should be good to go. There is an outstanding feature request to allow the sequence name to be explicitly set per-table, a la table_name and primary_key. Good luck. Please submit an enhancement request at http:// dev.rubyonrails.com/newticket if you can''t get things working as you''d like. Best, jeremy
I''ve got this done for another adapter - unfortunately I''ve
been doing
other things and haven''t had time to clean things up. But here is the
quick version which should work if you need it ASAP
Inside active_record/connection_adapters/abstract_adapter.rb add the
following to the AbstractAdapter class
def set_generator_name(table_name, generator_name)
@generators[table_name] = generator_name
end
def clear_generator_name(table_name)
@generators.delete(table_name)
end
Also modify initialize and add the following
@generators = {}
Then inside the Postgresql Adapter
(active_record/connection_adapters/postgresql_adapter.rb) make the
following mod to last_insert_id
change
sequence_name = "#{table}_#{column || ''id''}_seq"
to
sequence_name = @generators[table] || "#{table}_#{column ||
''id''}_seq"
You now have the ability to make the following call from you app to make
your generator name for any table whatever you wish
ActiveRecord::Base.connection.set_generator_name( table_name,
generator_name )
This obviously needs to be called after
ActiveRecord::Base::establish_connection(.....)
Like I said - it works for my firebird adapter I just don''t have the
time to make it look pretty at the moment :) I haven''t tested it on
postgresql but I "stole" the generator concept for firebird from
postgresql so it should work fine.
John W Higgins
wishdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org