Hi all, I have a Product that has_many :votes. I am trying to load the product and eagerly load the votes within the same SQL, as follows: @product = Product.find_by_id(id, :include => [:votes]) This generates the following SQL, which is perfectly valid except the Where clause has an unqualified id column. SELECT products.main_image_id AS t0_r6, products.id AS t0_r7, products.description AS t0_r8, products.category_id AS t0_r10, products.summary AS t0_r9, votes.ranking AS t1_r0, products.tstamp AS t0_r11, votes.product_id AS t1_r1, products.developer_id AS t0_r12, products.name AS t0_r0, votes.member_id AS t1_r2, products.created_at AS t0_r13, products.updated_at AS t0_r1, votes.id AS t1_r3, products.state AS t0_r14, products.title AS t0_r2, products.code AS t0_r3, products.import_id AS t0_r4, products.description_html AS t0_r5 FROM products LEFT OUTER JOIN votes ON votes.product_id = products.id WHERE id = ''4'' Since both votes and products have an id column, MySQL complains with "ERROR 1052 (23000): Column ''id'' in where clause is ambiguous". If I change the Where to products.id = ''4'', everything works fine. Is this a known bug? I am on rails 0.12.1. Thanks!
It''s not a bug, it''s a feature. :] It''s documented on the ActiveRecord::Associations page of the api docs. Cheers, Tyler On 6/6/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote:> Hi all, > > I have a Product that has_many :votes. I am trying to load the product > and eagerly load the votes within the same SQL, as follows: > > @product = Product.find_by_id(id, :include => [:votes]) > > This generates the following SQL, which is perfectly valid except the > Where clause has an unqualified id column. > > SELECT > products.main_image_id AS t0_r6, products.id AS t0_r7, > products.description AS t0_r8, products.category_id AS t0_r10, > products.summary AS t0_r9, votes.ranking AS t1_r0, > products.tstamp AS t0_r11, votes.product_id AS t1_r1, > products.developer_id AS t0_r12, > products.name AS t0_r0, votes.member_id AS t1_r2, > products.created_at AS t0_r13, products.updated_at AS t0_r1, votes.id AS > t1_r3, > products.state AS t0_r14, products.title AS t0_r2, products.code > AS t0_r3, products.import_id AS t0_r4, products.description_html AS t0_r5 > > FROM > products LEFT OUTER JOIN votes ON votes.product_id > products.id WHERE id = ''4'' > > Since both votes and products have an id column, MySQL complains with > "ERROR 1052 (23000): Column ''id'' in where clause is ambiguous". If I > change the Where to products.id = ''4'', everything works fine. > > Is this a known bug? I am on rails 0.12.1. > > Thanks! > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Actually when calling these generated attributes finder, the problematic condition is generated by the framework ... it should make sure the condition is not ambiguous shouldn''t it ? jean On 6/6/05, Tyler Kiley <tyler.kiley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> It''s not a bug, it''s a feature. :] It''s documented on the > ActiveRecord::Associations page of the api docs. > > Cheers, > > Tyler > > On 6/6/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote: > > Hi all, > > > > I have a Product that has_many :votes. I am trying to load the product > > and eagerly load the votes within the same SQL, as follows: > > > > @product = Product.find_by_id(id, :include => [:votes]) > > > > This generates the following SQL, which is perfectly valid except the > > Where clause has an unqualified id column. > > > > SELECT > > products.main_image_id AS t0_r6, products.id AS t0_r7, > > products.description AS t0_r8, products.category_id AS t0_r10, > > products.summary AS t0_r9, votes.ranking AS t1_r0, > > products.tstamp AS t0_r11, votes.product_id AS t1_r1, > > products.developer_id AS t0_r12, > > products.name AS t0_r0, votes.member_id AS t1_r2, > > products.created_at AS t0_r13, products.updated_at AS t0_r1, votes.id AS > > t1_r3, > > products.state AS t0_r14, products.title AS t0_r2, products.code > > AS t0_r3, products.import_id AS t0_r4, products.description_html AS t0_r5 > > > > FROM > > products LEFT OUTER JOIN votes ON votes.product_id > > products.id WHERE id = ''4'' > > > > Since both votes and products have an id column, MySQL complains with > > "ERROR 1052 (23000): Column ''id'' in where clause is ambiguous". If I > > change the Where to products.id = ''4'', everything works fine. > > > > Is this a known bug? I am on rails 0.12.1. > > > > Thanks! > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks - I figured Rails couldn''t have a bug :> I changed @product = Product.find_by_id(id , :include => [:votes]) To @product = Product.find(id , :include => [:votes]) And it works now. I wonder: how did Rails know to disambiguate the find but was not able to do the same with find_by_id? Thanks! Tyler Kiley wrote: It''s not a bug, it''s a feature. :] It''s documented on the ActiveRecord::Associations page of the api docs. Cheers, Tyler On 6/6/05, David Teare wrote: Hi all, I have a Product that has_many :votes. I am trying to load the product and eagerly load the votes within the same SQL, as follows: @product = Product.find_by_id(id, :include => [:votes]) This generates the following SQL, which is perfectly valid except the Where clause has an unqualified id column. SELECT products.main_image_id AS t0_r6, products.id AS t0_r7, products.description AS t0_r8, products.category_id AS t0_r10, products.summary AS t0_r9, votes.ranking AS t1_r0, products.tstamp AS t0_r11, votes.product_id AS t1_r1, products.developer_id AS t0_r12, products.name AS t0_r0, votes.member_id AS t1_r2, products.created_at AS t0_r13, products.updated_at AS t0_r1, votes.id AS t1_r3, products.state AS t0_r14, products.title AS t0_r2, products.code AS t0_r3, products.import_id AS t0_r4, products.description_html AS t0_r5 FROM products LEFT OUTER JOIN votes ON votes.product_id products.id WHERE id = ''4'' Since both votes and products have an id column, MySQL complains with "ERROR 1052 (23000): Column ''id'' in where clause is ambiguous". If I change the Where to products.id = ''4'', everything works fine. Is this a known bug? I am on rails 0.12.1. Thanks! _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails