Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] Fixed non-standard SQL generated by preloading has_and_belongs_to_many associations

#394

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.

Reported by Antonio Cangiano · June 11th, 2008 @ 08:42 PM

State: resolved
Milestone: 2.1.1
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 2.1.1
    • State changed from new to open
    • Assigned user set to Jeremy Kemper

    June 12th, 2008 @ 01:28 AM

  2. Repository
    Repository
    • State changed from open to resolved

    (from [4689496b525526a776004c4986d98b0ce62309d5]) Fixed non-standard SQL generated by preloading has_and_belongs_to_many association. [#394 state:resolved]

    http://github.com/rails/rails/co...

    June 12th, 2008 @ 01:36 AM

  3. Don McClean
    Don McClean
    • Tag set 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

    July 27th, 2008 @ 12:30 AM

  4. Antonio Cangiano
    Antonio Cangiano

    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

    July 28th, 2008 @ 01:18 AM

  5. Don McClean
    Don McClean

    Antonio,

    Since removing the 'as' still would be standard SQL, it

    would be appreciated if you could remove it.

    Thanks and Regards,

    Don McClean

    July 30th, 2008 @ 03:05 PM

  6. Brent Miller
    Brent Miller

    Since this ticket seems to be closed, the "as" issue is being addressed in ticket #1405 [PATCH] Fixed non-standard SQL generated by preloading has_and_belongs_to_man...

    November 20th, 2008 @ 05:02 PM