Skip to content

FactoryGirl with Oracle enhanced adapter – an attribute of name ‘id’ is not assigned

My question and answer are also posted on StackOverflow

I have an Oracle test table with ID column that is not a primary key, not an autoincrement and with no sequence defined. It only has NOT NULL defined. The corresponding structure in development schema is an Oracle View rather than a table with a primary key defined against the ID column.

My factory definition looks like:

These tests fails

I’ve added attr_accessible :id to my model (just in case), but with no luck.

I’ve also defined id= method in my model def id= id; @id = id; end. The call is made to the method and @id is written, but looks like it is overwritten later in the callstack (between FactoryGirl and ActiveRecord).

Other attributes are normally assigned.

After a bit of digging – I found the I found the culprit:

The write method in ActiveRecord::AttributeMethods::Write module has the line:

It means that if the attribute name to be set has the name of ‘id’, pick the primary_key as the name of the attribute to assign the new value to. And since in my test table there is no primary_key returned by the oracle_enhanced_adapter (rightfully), the whole write operation is skipped.

My solution was to override the write method (only when running tests) skipping that line so that the id is written to.

Posted in Ruby.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

or, reply to this post via trackback.