[PATCH] Fixed non-standard SQL generated by preloading has_and_belongs_to_many associations
Reported by Antonio Cangiano | June 11th, 2008 @ 08:42 PM | in 2.1.1
Preloading has_and_belongs_to_many associations generates non-standard SQL which breaks standard-compliant databases like DB2 and Oracle.
Example:
class Project < ActiveRecord::Base
has_and_belongs_to_many :developers
end
class Developer < ActiveRecord::Base
has_and_belongs_to_many :projects
end
p Project.find(:all, :include => :developers)
The previous code generates queries along the lines of:
SELECT developers.*, t0.project_id as _parent_record_id FROM developers INNER JOIN developers_projects as t0 ON developers.id = t0.developer_id WHERE (t0.project_id IN (1,2))
An SQL identifier cannot begin with an underscore, and this is true for all three versions of the standard (92, 99 and 2003). You can verify the non-conformity of the generated SQL through this validator: http://developer.mimer.com/valid...
In the attached patch, I renamed _parent_record_id with the_parent_record_id.
Please note that no further tests are required given that the test cases in cases/associations_test.rb already cover this specific issue.
Comments and changes to this ticket
-
Jeremy Kemper June 12th, 2008 @ 01:28 AM
- → Milestone changed from to 2.1.1
- → State changed from new to open
- → Assigned user changed from to Jeremy Kemper
-

Repository June 12th, 2008 @ 01:36 AM
- → State changed from open to resolved
(from [4689496b525526a776004c4986d98b0ce62309d5]) Fixed non-standard SQL generated by preloading has_and_belongs_to_many association. [#394 state:resolved]
-

Don McClean July 27th, 2008 @ 12:30 AM
- → Tag changed from to activerecord bug has_and_belongs_to_many patch standard-sql
I manually patched my source with your fix and still got an
error. I am running Oracle 9.2. The second portion of the
above example 'as t0' was still causing an error. Removing the
'as' fixed the problem.
Regards,
Don McClean
-

Antonio Cangiano July 28th, 2008 @ 01:18 AM
Don, this must be an Oracle peculiarity. Using the "as t0" is standard SQL. It's OK to omit it though, if it helps Oracle, because omitting it respects the standard too
-

Don McClean July 30th, 2008 @ 03:05 PM
Antonio,
Since removing the 'as' still would be standard SQL, it
would be appreciated if you could remove it.
Thanks and Regards,
Don McClean
-
Brent Miller November 20th, 2008 @ 05:02 PM
Since this ticket seems to be closed, the "as" issue is being addressed in ticket #1405
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
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
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".
