Problems when using custom primary key value

Hi all,

I am having a bit of trouble getting rails to interact with a legacy
postgresql database.
The problem essentially arises because I define the primary key value
using some custom logic before making an insert, but when I
call .save! I get the following error:
ActiveRecord::StatementInvalid: PGError: ERROR: relation
“i_bpartner_custom_i_bpartner_custom_id_seq” does not exist
: SELECT currval(‘I_BPartner_Custom_I_BPartner_Custom_ID_seq’)
from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
connection_adapters/abstract_adapter.rb:150:in log' from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/ connection_adapters/postgresql_adapter.rb:407:inexecute’
from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
connection_adapters/postgresql_adapter.rb:782:in select_raw' from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/ connection_adapters/postgresql_adapter.rb:769:inselect’
from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
connection_adapters/abstract/database_statements.rb:7:in
select_all_without_query_cache' from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/ connection_adapters/abstract/query_cache.rb:55:inselect_all’
from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
connection_adapters/abstract/database_statements.rb:13:in select_one' from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/ connection_adapters/abstract/database_statements.rb:19:inselect_value’
from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/
connection_adapters/postgresql_adapter.rb:763:in last_insert_id' from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/ connection_adapters/postgresql_adapter.rb:390:ininsert’

What seems to be happening is that after making the insert, rails asks
postgresql what the new primary key was but postgresql what the
primary key was but postgresql doesn’t (and shouldn’t) know.
The culprit is the insert method in the
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter class
(activerecord-2.0.2/lib/active_record/connection_adapters/
postgresql_adapter.rb:390). It calls the last_insert_id method (in the
same class) which it really shouldn’t.
Does anyone know how to get around this? The only thing I can think of
is to patch this class at run-time to figure out whether it should
already know the new ID or not and act accordingly but my ruby isn’t
good enough to get access to that class. Could anyone tell me how i
might intercept that method?

Thanks
Mark