You do not have access to this ticket

This project is archived and is in readonly mode.

#394 ✓resolved
Antonio Cangiano

[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

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages