This project is archived and is in readonly mode.

#258 ✓resolved
Zyclops

Eager loading optimisation misses if order specified

Reported by Zyclops | May 27th, 2008 @ 03:02 AM | in 2.1.1

When no conditions are passed into find, 2 individual queries are run. One to fetch from the table people and the other from addresses. When you provide some conditions that reference the table, the queries are combined into a single query with left outer joins.

The issue is that when you only specify an order, the individual queries still occur and the ordering throws an error as the table is not aliased.

I presume the expected behavior should be that order is checked and any included tables are left joined and aliased.

class Person
  belongs_to :address
end

class Address
end

def my_action
  @results =Person.find(:all, :include => [:address], :order => 'addresses.street_name DESC'
end

Comments and changes to this ticket

  • Jeremy Kemper

    Jeremy Kemper May 27th, 2008 @ 03:35 AM

    • State changed from “new” to “incomplete”

    On what database? This works for me. Please attach a failing test case.

  • Yannick Lecaillez

    Yannick Lecaillez June 3rd, 2008 @ 08:20 AM

    Got same problem (rev. 9250).

    With the code published by Zyclops, try:

    Person.find(:first).address

    This generate two queries:

    SELECT * FROM `persons` LIMIT 1

    SELECT * FROM `addresses` WHERE (`addresses`.id = 1 )

    When i expected only one with a LEFT JOIN.

  • Jeremy Kemper

    Jeremy Kemper June 3rd, 2008 @ 08:57 AM

    Yannick, you don't have an :order referencing the :included table. Two queries are expected.

  • Stefano C.

    Stefano C. June 25th, 2008 @ 04:37 PM

    • Tag set to 2.1, activerecord, bug, eagerloading, named_scope, order

    I have the same problem (Rails 2.1.0 via gems) with a named_scope:

    class Release
      belongs_to :movie
      named_scope :by_title, :order => "movies.title"
    end
    
    class Movie
      has_many :releases
    end
    
    Release.by_title.find(:all)
    # PGError: ERROR:  missing FROM-clause entry for table "movies"
    

    If I add a dummy :conditions key (e.g. "movies.title is not null") to the named_scope, it works.

    I've created a test for this particular case and it passes in HEAD. I think the bug was resolved in #372.

  • Jeremy Kemper

    Jeremy Kemper June 25th, 2008 @ 06:43 PM

    • Milestone set to 2.1.1
    • Assigned user set to “Jeremy Kemper”

    Stefano, you don't reference the association in your example.

  • Stefano C.

    Stefano C. June 25th, 2008 @ 07:31 PM

    Woops, I was trying the various combinations and I pasted the wrong one :-)

    Here is the right one, straight from my project:

    class Release
      belongs_to :movie
      named_scope :current, :conditions => ["status in (?)", [0, 1, 2]]
      named_scope :by_title, :order => "movies.title"
    end
    
    class Movie
      has_many :releases
    end
    
    Release.current.by_title.find(:all, :include => :movie)
    # SELECT * FROM "releases" WHERE (status in (0,1,2)) ORDER BY movies.title
    # PGError: ERROR:  missing FROM-clause entry for table "movies"
    
  • Ryan Bigg

    Ryan Bigg April 10th, 2010 @ 08:44 AM

    In Rails 2.3.5 this seems to eager load for me:

    >> Release.current.by_title.find(:all, :include => :movie)
    
      Release Load Including Associations (0.3ms)   SELECT "releases"."id" AS t0_r0, "releases"."movie_id" AS t0_r1, "releases"."created_at" AS t0_r2, "releases"."updated_at" AS t0_r3, "releases"."status" AS t0_r4, "movies"."id" AS t1_r0, "movies"."title" AS t1_r1, "movies"."created_at" AS t1_r2, "movies"."updated_at" AS t1_r3 FROM "releases" LEFT OUTER JOIN "movies" ON "movies".id = "releases".movie_id WHERE (status in (0,1,2)) ORDER BY movies.title
    
  • Ryan Bigg

    Ryan Bigg April 12th, 2010 @ 08:52 AM

    • State changed from “incomplete” to “resolved”

    Marking as resolved, appears to be fixed in 2.3.5.

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

Pages