I am mapping ActiveRecord classes to an existing Oracle database. This
means I cannot change the database schema to fit ActiveRecord
conventions.
Predictably, I have gotten into trouble: I have several join tables
that do not have a primary key column, and therefore no number sequence
defined. ActiveRecord insists on requiring a sequence, and gets unhappy
when there is none to find.
Is there a way to tell ActiveRecord not to expect a sequence number
(and no primary key)?
I asked the same question months ago and got no responses. Even
though I said :id => false during my migration, AR complained
(ORA-02289: sequence does not exist) that it needed a sequence. I
created the sequence manually just for kicks, and AR complained
(ORA-00904: invalid identifier) that it couldn't insert into an id
column. Duh, there is no id column because we don't want one. So, I
looked further into the docs for AR::Base and tried set_sequence_name
(nil) and got ORA-00936: missing expression: select .nextval from
dual. Thus, AR was still trying to use a sequence without a name. I
removed set_sequence_name and tried using set_primary_key to one of
my columns, and AR inserted the sequence number into that column.
So, I do not know how to tell AR not to use a sequence number. But I
thought I'd document what I've tried in case anyone else wants to
compare notes.
Well, I somehow got past this problem in my code. I guess I just wasn't
properly assigning the id somehow. Got my form right and as long as an
id is set on the new record, the sequence will not be called.
Here's the relevant code from the create method in ActiveRercord's
base.rb:
if self.id.nil? &&
connection.prefetch_primary_key?(self.class.table_name)
self.id =
connection.next_sequence_value(self.class.sequence_name)
end
Maybe you can try overriding that method with a version of your own to
get around your problem with composite keys. Or maybe something like
http://compositekeys.rubyforge.org/ would help?
Well, I somehow got past this problem in my code. I guess I just wasn't
properly assigning the id somehow. Got my form right and as long as an
id is set on the new record, the sequence will not be called.
Correct, that's the expected/correct behavior -- so long as you define the PK properly, and set it yourself, it won't try to use a sequence.