This project is archived and is in readonly mode.

#304 ✓invalid
Mark Roghelia

Preloading a "has_and_belongs_to_many" generates invalid SQL (with Oracle at least)

Reported by Mark Roghelia | June 2nd, 2008 @ 10:21 PM

I am using Rails 2.1 with an Oracle database on OSX.

I have a UserProfile model with a has_and_belongs_to_many association of UserRoles.

I executed the following:

UserProfile.find(1, :include => :user_roles)

I got this error:

/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:147:in `log': OCIError: ORA-00911: invalid character: SELECT user_role.*, t0.user_profile_id as _parent_record_id FROM user_role INNER JOIN user_profile_user_role as t0 ON = t0.user_role_id WHERE (t0.user_profile_id IN (1)) (ActiveRecord::StatementInvalid)

from /Library/Ruby/Gems/1.8/gems/activerecord-oracle-adapter- `execute'

from /Library/Ruby/Gems/1.8/gems/activerecord-oracle-adapter- `select'

from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'

from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `select_all'

from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:582:in `find_by_sql'

from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1341:in `find_every'

from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:536:in `find_without_pagination'

from /Users/mark/src/msd/vendor/plugins/paginating_find/lib/paginating_find.rb:102:in `find'

... 16 levels...

from /Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/runner.rb:47

from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'

from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `require'

from ./script/runner:3

I pasted this SQL into Oracle's SQL Developer and played with it there to work out what was wrong. There were actually two different problems.

The first syntax problem was the "as" in the inner join to user_profile_user_role. I patched the code to remove this "as" and got a second error. This time it was the column alias "_parent_record_id". In Oracle a name cannot start with an underscore. I replaced "_parent_record_id" with "x_parent_record_id" and things worked.

Comments and changes to this ticket

  • Pratik

    Pratik June 4th, 2008 @ 07:28 PM

    • State changed from “new” to “invalid”

    Patch needs tests.


  • Duke

    Duke August 5th, 2008 @ 12:27 AM

    • Tag set to activerecord, bug, eager_loading, has_and_belongs_to_many, oracle, patch

    After more than an hour of trying, I'm still unable to get Activerecord's unit tests running on my Oracle db. However, I'm willing to bet that if I did, they would fail due to this bug.

    I'm giving up and just applying the patch. But, for any brave soul willing to carry the torch, this may help:

    Good luck, and this really needs to get into rails if we care at all about oracle working.

  • bshand

    bshand November 30th, 2009 @ 03:39 PM

    This issue is fixed in Rails 2.3.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>


Referenced by