开发者

Over-quote problem with ruby, activerecord and composite_primary_keys on oracle

I am getting an Oracle error because of the SQL being generated for an association between tables. There is no开发者_C百科 compound primary key involved, but this problem did not occur before adding the composite_primary_keys gem.

The association's foreign key is being added to the SQL like so:

SELECT "FOO".* FROM "FOO"  WHERE "FOO"."Bar_id" = 1234

The column "bar_id" does exist. However, when we put "Bar_id" in quotes this way, Oracle takes us seriously, and goes all case sensitive.

If only we were generating this:

SELECT "FOO".* FROM "FOO"  WHERE "FOO".Bar_id = 1234

or this:

SELECT "FOO".* FROM "FOO"  WHERE "FOO"."BAR_ID" = 1234

We'd be fine.

Here's the error we're getting:

ActiveRecord::StatementInvalid: NativeException: java.sql.SQLException: ORA-00904: "FOO"."Bar_id": invalid identifier

I actually fixed (read, "hacked") this for version 3.1.0 of the composite_primary_keys gem, but now that I need to upgrade to 4.0.0, the code changes have been so significant that the simple hacks I used before are no longer so obvious.

Any idea how to fix this?

Versions of Stuff Used:

  • composite_primary_keys 4.0.0
  • activerecord 3.1.0
  • activerecord-oracle_enhanced-adapter 1.4.0
  • jruby 1.6.3


Ok, I think I found the problem. (Hat tip to mu_is_too_short for putting me on the right track.)

While foot.uninjured? { pull_trigger }...

Because our tables don't follow ActiveRecord convention for primary and foreign keys, but end with "_seq_id" instead of just "_id", we wrote an override for ActiveSupport::Inflector.foreign_key which basically demodulizes the model's class name and tacks "_seq_id" on the end. The demodulize method is the one that leaves us with "Bar" which then becomes "Bar_seq_id".

So, by upcasing the whole inflected foreign key, we can make this problem go away. Totally weird.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜