This project is archived and is in readonly mode.

#5524 open
Stephen Heuer

Not unique table/alias/ambiguous column name with chained named_scopes

Reported by Stephen Heuer | September 1st, 2010 @ 10:07 PM

Something that worked in rails 2.3.8 and below that does not work in 2.3.9.

Loading development environment (Rails 2.3.8)
ree-1.8.7-2010.02 > Top.first.bottoms_in_version.in_location(Building.last)
 => [#<Bottom id: 1, middle_id: 1, version: 1, location_type: "Building", location_id: "2", created_at: "2010-09-01 20:45:08", updated_at: "2010-09-01 20:45:08">, #<Bottom id: 2, middle_id: 1, version: 1, location_type: "Room", location_id: "4", created_at: "2010-09-01 20:45:08", updated_at: "2010-09-01 20:45:08">]
Loading development environment (Rails 2.3.9)
ree-1.8.7-2010.02 > Top.first.bottoms_in_version.in_location(Building.last)
ActiveRecord::StatementInvalid: SQLite3::SQLException: ambiguous column name: middles.id: SELECT "bottoms"."id" AS t0_r0, "bottoms"."middle_id" AS t0_r1, "bottoms"."version" AS t0_r2, "bottoms"."location_type" AS t0_r3, "bottoms"."location_id" AS t0_r4, "bottoms"."created_at" AS t0_r5, "bottoms"."updated_at" AS t0_r6, "middles"."id" AS t1_r0, "middles"."top_id" AS t1_r1, "middles"."version" AS t1_r2, "middles"."created_at" AS t1_r3, "middles"."updated_at" AS t1_r4, "tops"."id" AS t2_r0, "tops"."name" AS t2_r1, "tops"."version" AS t2_r2, "tops"."created_at" AS t2_r3, "tops"."updated_at" AS t2_r4 FROM "bottoms"  LEFT OUTER JOIN "middles" ON "middles".id = "bottoms".middle_id  LEFT OUTER JOIN "tops" ON "tops".id = "middles".top_id  INNER JOIN "middles" ON "bottoms".middle_id = "middles".id    WHERE (((((`location_type` = 'Room' AND `location_id` IN (4,5,6)) OR (`location_type` = 'Building' AND `location_id` IN (2))) AND `tops`.version == `bottoms`.version) AND (`bottoms`.`version` = 1)) AND (("middles".top_id = 1)))

Throws errors in sqlite3 and mysql:
Mysql::Error: Not unique table/alias
SQLite3::SQLException: ambiguous column name

I have attached a rails app that demonstrates the problem.

rake db:migrate
rake db:seed
script/console
> Top.first.bottoms_in_version.in_location(Building.last)

Comments and changes to this ticket

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:49 PM

    • State changed from “new” to “open”
    • Tag changed from 2.3.9, bug, joins, named_scope to 239, bug, joins, named_scope

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:49 PM

    • State changed from “open” to “stale”
  • Dan

    Dan February 20th, 2011 @ 08:52 AM

    • State changed from “stale” to “open”

    [state:open]

    Still an issue in Rails 3.0.4 As above run
    rake db:migrate
    rake db:seed
    rails c

    Loading development environment (Rails 3.0.4)

    Category.good.joins(:items) ActiveRecord::StatementInvalid: SQLite3::SQLException: ambiguous column name: name: SELECT "categories".* FROM "categories" INNER JOIN "items" ON "items"."category_id" = "categories"."id" WHERE (name = 'Good Stuff')

    As a workaround, if you recreate the scope as follows:
    scope :good, :conditions => ["categories.name = 'Good Stuff'"]

    then it works fine. I'm not sure what implications this would have on intersection between scopes, but in this demo

    Category.good.important.joins(:items)

    does work.

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