#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 user_role.id = t0.user_role_id WHERE (t0.user_profile_id IN (1)) (ActiveRecord::StatementInvalid)

from /Library/Ruby/Gems/1.8/gems/activerecord-oracle-adapter-1.0.0.9216/lib/active_record/connection_adapters/oracle_adapter.rb:221:in `execute'

from /Library/Ruby/Gems/1.8/gems/activerecord-oracle-adapter-1.0.0.9216/lib/active_record/connection_adapters/oracle_adapter.rb:516:in `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.

    Thanks.

  • Duke

    Duke August 5th, 2008 @ 12:27 AM

    • → Tag changed from “” 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:http://media.railscasts.com/videos/113_contributing_to_rails_with_git.mov

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

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

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

Source available from github

Repository is at http://github.com/rails/rails

Check out the development master (Edge Rails):

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..

Shared Ticket Bins

People watching this ticket

Attachments