Ruby ActiveRecord and Oracle: Auto Incrementing Primary Keys using Triggers

Posted by Jamie on January 30, 2008

One of the most obvious limitations that I have faced when using ActiveRecord is it’s ability to adapt to legacy systems, especially Oracle databases. Many of the Oracle databases that I have used employ triggers to populate numeric primary key columns when data is inserted into the database tables. While this is a very nice and easy way to hide the mechanisms used to identify the data from the application developers when they are using straight DML, when you try to use ActiveRecord on these tables, ESPECIALLY when dealing with large object (LOB) fields, you get into some real hair-pulling situations.

ActiveRecord handles LOB inserts in two steps:

  1. The data is inserted into the database and the LOB field is initialized with an empty lob.
  2. The record is updated with the provided LOB data, replacing the empty lob.

As part of step 1 above, ActiveRecord pre-fetches the sequence that is used to populate the primary key field of the table. If you then have a trigger on that table that updates the incoming primary key column with the next sequence value, your Ruby object and the data inserted into the database are out of sync. Therefore, in step
2 of the LOB insert process described above, ActiveRecord is attempting to update a row with a primary key value that does not exist and no LOB values are ever populated.

To prevent this, I created the following code that alters ActiveRecord::Base and ActiveRecord::ConnectionAdapters::OracleAdapter to use the trigger-generated key (via the Oracle ‘RETURNING’ clause) in the AR object.

module ActiveRecord
module ConnectionAdapters #:nodoc:
class OracleAdapter < AbstractAdapter
def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
cursor = @connection.parse(sql)
log(sql, name) { cursor.exec(0) }
# return the primary key value for the record just inserted

class Base
# Creates a record with values matching those of the instance attributes and
# returns its id.
def create
if && connection.prefetch_primary_key?(self.class.table_name) = connection.next_sequence_value(self.class.sequence_name)

quoted_attributes = attributes_with_quotes

statement = if quoted_attributes.empty?
“INSERT INTO #{self.class.quoted_table_name} ” +
“(#{quoted_column_names.join(‘, ‘)}) ” +
“VALUES(#{quoted_attributes.values.join(‘, ‘)}) returning #{self.class.primary_key} into :1”
end = connection.insert(statement, “#{} Create”,
self.class.primary_key,, self.class.sequence_name)

@new_record = false

Now, this is really, really rough. I am using the 2.x version of ActiveRecord, and any updates to these sections in future releases will break my changes. It does, however, work for me right now. As I have time I will refactor this and increase its maintainablility.



Use this link to trackback from your own site.


Leave a response