# Problems when using custom primary key value

**URL:** <https://www.ruby-forum.com/t/problems-when-using-custom-primary-key-value/133652>\
**Category:** Rails\
**Created:** [April 2, 2008, 10:53am UTC](https://www.ruby-forum.com/t/problems-when-using-custom-primary-key-value/133652 "2008-04-02T10:53:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 2, 2008, 10:53am UTC](https://www.ruby-forum.com/t/problems-when-using-custom-primary-key-value/133652/1 "2008-04-02T10:53:19Z")

</div>

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:in`execute’  
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:in`select’  
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:in`select\_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:in`select\_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:in`insert’  
…  
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
