Yuri Leikind
2008-Feb-06 10:28 UTC
weird behavior of belongs_to referencing a model with set_table_name : a bug?
Hello all, Since nobody answered the letter below in rubyonrails-talk, I''ve taken the courage to repost it here. I looks like a bug, but I just refuse to believe it as it is such a basic functionality of Active Record. ---------- Forwarded message ---------- From: Yuri Leikind <yuri.leikind@gmail.com> Date: 5 feb. 2008 13:24 Subject: weird behavior of belongs_to referencing a model with set_table_name To: rubyonrails-talk@googlegroups.com Hello all, I am experiencing some really weird behavior of belongs_to with a model where the table name is set by set_table_name I have two simple models: === file legal_entity.rb class LegalEntity < ActiveRecord::Base set_table_name ''legalentities'' end === file legal_entity_person.rb class LegalEntityPerson < ActiveRecord::Base set_table_name ''legalentities_people'' belongs_to :legal_entity, :foreign_key => '' legalentity_id'' end ==== If I find a LegalEntityPerson object with :include => :legal_entity it works as expected: $ ./script/runner ''a = LegalEntityPerson.find(:first, :include => :legal_entity); p a.legal_entity'' #<LegalEntity id: 1, name: "Some Company", legalentitytype_id: 3> and looking a the log I see the select method with the 2 tables joined, it''s fine. Now, if I drop :include => :legal_entity, a.legal_entity returns nil: $ ./script/runner ''a = LegalEntityPerson.find(:first); p a.legal_entity'' nil And what is interesting is that there is no SQL query to the DB, that is, there is SELECT * FROM `legalentities_people` LIMIT 1 for the find method, and nothing else, a.legal_entity silently returns nil without any warnings or complaints. If I rewrite my models like this following naming conventions: === file legalentity.rb class Legalentity < ActiveRecord::Base end === file legal_entity_person.rb class LegalEntityPerson < ActiveRecord::Base set_table_name ''legalentities_people'' belongs_to :legalentity end ==== it works: $ ./script/runner ''a = LegalEntityPerson.find(:first); p a.legalentity'' #<Legalentity id: 1, name: "Some Company", legalentitytype_id: 3> BUT!!! If I specify :foreign_key explicitly like this: belongs_to :legalentity, :foreign_key => '' legalentity_id'' it doesn''t work again: Is there any reason behind this behavior or is it a bug? I am using Rails 2.0.2, mysql 5.0.51a, all running on OS X, and with the following sql code you can reproduce it: CREATE TABLE `legalentities` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(255) NOT NULL, `legalentitytype_id` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; INSERT INTO `legalentities` VALUES (1,''Some Company'',3); CREATE TABLE `legalentities_people` ( `id` int(10) unsigned NOT NULL auto_increment, `legalentity_id` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; INSERT INTO `legalentities_people` VALUES (1,1); -- Best regards, Yuri Leikind --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Koziarski
2008-Feb-09 08:18 UTC
Re: weird behavior of belongs_to referencing a model with set_table_name : a bug?
> Hello all, > > > Since nobody answered the letter below in rubyonrails-talk, I''ve taken the > courage to repost it here. > I looks like a bug, but I just refuse to believe it as it is such a basic > functionality of Active Record.It does seem strange. Can you reproduce the bug in the active record tests? If so you can open a trac ticket and we can take a look at it. -- Cheers Koz --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Cláudio Caseiro
2008-Feb-11 18:04 UTC
Re: weird behavior of belongs_to referencing a model with se
Hi, I think I have a related problem (that I was about to post) ... I have the following problem With a legacy Database: class User < ActiveRecord::Base set_table_name :jos_users has_many :orders has_many :order_items, :through => :orders end class Order < ActiveRecord::Base set_table_name :jos_vm_orders set_primary_key :order_id has_many :order_items belongs_to :user end class OrderItem < ActiveRecord::Base set_table_name :jos_vm_order_item set_primary_key :order_item_id belongs_to :order end When I try to get all order items for a specific user I get an error:>> u=User.find(63).order_itemsActiveRecord::StatementInvalid: Mysql::Error: Unknown column ''jos_vm_orders.order_item_id'' in ''on clause'': SELECT jos_vm_order_item.* FROM jos_vm_order_item INNER JOIN jos_vm_orders ON jos_vm_order_item.order_id = jos_vm_orders.order_item_id WHERE ((jos_vm_orders.user_id = 63)) from /var/lib/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:150:in `log'' I don''t understand why rails is trying to get jos_vm_orders.order_item_id instead of jos_vm_orders.order_id, since the primary key of orders table os order_id. Can somebody help. Tahnks, Cláudio Caseiro -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2008-Feb-11 21:26 UTC
Re: weird behavior of belongs_to referencing a model with se
On 2/11/08, Cláudio Caseiro <ruby-forum-incoming@andreas-s.net> wrote:> > Hi, I think I have a related problem (that I was about to post) > > ... > > I have the following problem With a legacy Database: > > class User < ActiveRecord::Base > set_table_name :jos_users > > has_many :ordersYou need to add the option :foreign_key => "order_id" so has_many :orders, :foreign_key => "order_id"> has_many :order_items, :through => :orders > end > > class Order < ActiveRecord::Base > set_table_name :jos_vm_orders > set_primary_key :order_id > > has_many :order_items > belongs_to :user > end > > class OrderItem < ActiveRecord::Base > set_table_name :jos_vm_order_item > set_primary_key :order_item_id > > belongs_to :order > end > > > When I try to get all order items for a specific user I get an error: > >> u=User.find(63).order_items > ActiveRecord::StatementInvalid: Mysql::Error: Unknown column > ''jos_vm_orders.order_item_id'' in ''on clause'': SELECT jos_vm_order_item.* > FROM jos_vm_order_item INNER JOIN jos_vm_orders ON > jos_vm_order_item.order_id = jos_vm_orders.order_item_id WHERE > ((jos_vm_orders.user_id = 63)) > from > /var/lib/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:150:in > `log'' > > I don''t understand why rails is trying to get > jos_vm_orders.order_item_id instead of jos_vm_orders.order_id, since the > primary key of orders table os order_id. > > Can somebody help. > > Tahnks, > Cláudio Caseiro > -- > Posted via http://www.ruby-forum.com/. > > > >-- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Cláudio Caseiro
2008-Feb-12 10:10 UTC
Re: weird behavior of belongs_to referencing a model with se
With that foreign_key I still have the problem, but I don''t think I need any foreign_key. I''m in class User, so if I need any foreign_key in a has_many association it would be :user_id, but since that is the default I don''t need any. I think the problem is that the join is using the primary_key defined in the OrderItem class for the order table, what is not correct. I changed that to "set_primary_key :foo" and I get ActiveRecord::StatementInvalid: Mysql::Error: Unknown column ''jos_vm_orders.foo'' in ''on clause'': SELECT `jos_vm_order_item`.* FROM `jos_vm_order_item` INNER JOIN jos_vm_orders ON jos_vm_order_item.order_id = jos_vm_orders.foo WHERE ((`jos_vm_orders`.user_id = 63)) Any help? Thanks, Claudio Caseiro Rick Denatale wrote:> On 2/11/08, Cl�udio Caseiro <ruby-forum-incoming@andreas-s.net> wrote: >> has_many :orders > You need to add the option :foreign_key => "order_id" > > so > has_many :orders, :foreign_key => "order_id" > >> >> ActiveRecord::StatementInvalid: Mysql::Error: Unknown column >> primary key of orders table os order_id. >> >> Can somebody help. >> >> Tahnks, >> Cl�udio Caseiro >> -- >> Posted via http://www.ruby-forum.com/. >> >> > >> > > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Jarkko Laine
2008-Feb-12 11:33 UTC
Re: weird behavior of belongs_to referencing a model with se
On 12.2.2008, at 12.10, Cláudio Caseiro wrote:> > With that foreign_key I still have the problem, but I don''t think I > need > any foreign_key. > I''m in class User, so if I need any foreign_key in a has_many > association it would be :user_id, but since that is the default I > don''t > need any. > > I think the problem is that the join is using the primary_key > defined in > the OrderItem class for the order table, what is not correct. I > changed > that to "set_primary_key :foo" and I get > > ActiveRecord::StatementInvalid: Mysql::Error: Unknown column > ''jos_vm_orders.foo'' in ''on clause'': SELECT `jos_vm_order_item`.* FROM > `jos_vm_order_item` INNER JOIN jos_vm_orders ON > jos_vm_order_item.order_id = jos_vm_orders.foo WHERE > ((`jos_vm_orders`.user_id = 63)) > > Any help?Can you test whether you have the same problems if you change your database to use the standard Rails naming scheme, i.e. so that you don''t have to use any of the table_name, set_primary_key etc. methods? I''m not suggesting that you''d have to do it in your actual product, but it could help figuring out whether some of them is breaking the scheme. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi