szimek
2008-Jun-13 14:25 UTC
Rails 2.1.0 - find with :include and missing JOIN in SQL query
Hi, We''re trying to rewrite veeeery old Rails app that we successfully managed to move to 1.2.6 and now we''re trying with 2.1.0. One of the problems is caused by :include in AR.find. Rails 1.2.6 generates totally different SQL query than 2.1.0. Here''s Rails find query and generated SQL queries: Foo.find(:all, :include => :ticket, :conditions => (''project_id => 1'')) Rails 1.2.6 SELECT foos.`id` AS t0_r0 ... FROM foos LEFT OUTER JOIN tickets ON tickets.id = foos.ticket_id WHERE (project_id = 1) # works fine Rails 2.1.0 SELECT * FROM `foos` WHERE (project_id = 1) # JOIN is missing and Rails complains about missing project_id column in foos table Foos table doesn''t have project_id column, but tickets table does. Foo belongs_to :ticket and Ticket belongs_to :project. Any idea how to fix it? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jun-13 14:45 UTC
Re: Rails 2.1.0 - find with :include and missing JOIN in SQL query
On 13 Jun 2008, at 15:25, szimek wrote:> > Hi, > > We''re trying to rewrite veeeery old Rails app that we successfully > managed to move to 1.2.6 and now we''re trying with 2.1.0. > > One of the problems is caused by :include in AR.find. Rails 1.2.6 > generates totally different SQL query than > 2.1.0. Here''s Rails find query and generated SQL queries: > > Foo.find(:all, :include => :ticket, :conditions => (''project_id => > 1'')) > > Rails 1.2.6 > SELECT foos.`id` AS t0_r0 ... FROM foos LEFT OUTER JOIN tickets ON > tickets.id = foos.ticket_id WHERE (project_id = 1) # works fine > > Rails 2.1.0 > SELECT * FROM `foos` WHERE (project_id = 1) # JOIN is missing and > Rails complains about missing project_id column in foos table > > Foos table doesn''t have project_id column, but tickets table does. Foo > belongs_to :ticket and Ticket belongs_to :project. > > Any idea how to fix it?You need to disambiguate your conditions (ie tickets.project_id = 1) Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
szimek
2008-Jun-14 12:14 UTC
Re: Rails 2.1.0 - find with :include and missing JOIN in SQL query
On Jun 13, 4:45 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 13 Jun 2008, at 15:25,szimekwrote: > > > > > > > > > Hi, > > > We''re trying to rewrite veeeery old Rails app that we successfully > > managed to move to 1.2.6 and now we''re trying with 2.1.0. > > > One of the problems is caused by :include in AR.find. Rails 1.2.6 > > generates totally different SQL query than > > 2.1.0. Here''s Rails find query and generated SQL queries: > > > Foo.find(:all, :include => :ticket, :conditions => (''project_id => > > 1'')) > > > Rails 1.2.6 > > SELECT foos.`id` AS t0_r0 ... FROM foos LEFT OUTER JOIN tickets ON > > tickets.id = foos.ticket_id WHERE (project_id = 1) # works fine > > > Rails 2.1.0 > > SELECT * FROM `foos` WHERE (project_id = 1) # JOIN is missing and > > Rails complains about missing project_id column in foos table > > > Foos table doesn''t have project_id column, but tickets table does. Foo > > belongs_to :ticket and Ticket belongs_to :project. > > > Any idea how to fix it? > > You need to disambiguate your conditions (ie tickets.project_id = 1) > > FredThanks! Works perfectly. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---