I was writing rspecs for one ActiveRecord model in my project and was connecting to different schema in which the corresponding mapping table was defined. The schema object (tables) definition was a legacy code, what I meant to say is I used the already existing object definition from that schema which was not using oracle sequence. There was no primary key for that table as well.
I was setting up the data for that table in my rspec using ActiveRecord insert.
e.g. AnotherSchema::TableObject.create(:ts => 123, :proj_code => "ABC", ...)
But, in Rails, when you try to insert the record with ActiveRecord Model, it always looks for a sequence, either a default one (Table_Object_seq) or explicitly defined one with set_sequence_name in the Model class.
So, it threw error as expected :-
OCIError: ORA-02289: sequence does not exist: select "Table_Object_seq
".nextval id from dual
I could have defined a sequence on this table only in the test database, but I googled around for a way to avoid oracle sequence in a model and I got this link :-
http://www.dixis.com/?p=127
I followed the suggestion and it worked for me :-)
What I did was :-
Monkeypatching OracleEnhanced Adapter's next_sequence_value method :-
and adding set_sequence_name to the Model class :-
set_sequence_name 'autogenerated'
It avoided the default oracle sequence look up while inserting new records with Rails ActiveRecord methods which solved my problem.
Subscribe to:
Post Comments (Atom)

Thanks for sharing this informative article. I am not aware of this issue and is about to use this in my code. You not only saved my time but also guided me what to use instead of it to solve the issue.
ReplyDeletesap erp training
Good to know that my small article solved your problem! Thanks for reading :-)
Delete