Rails 奇怪的 Rails 问题

hmilym · 2016年04月21日 · 最后由 tony612 回复于 2016年04月22日 · 2428 次阅读

为什么一样的查询,不一样的查询字符而已。结果却一个报错,一个正常,有谁遇到过类似问题吗? ruby version: 2.1.5 rails version: 3.2.18


class Contract < ActiveRecord::Base   #table in DB1
  belongs_to :partner_relationship,
          business_unit.rb  :class_name  => "Relationship",
                                          :foreign_key  => :partner_business_entity_id  
end

class Relationship < ActiveRecord::Base # table in DB2
end



[103] pry(main)> Contract.where("contract.name like 'ww.'").includes(:partner_relationship)
  SQL (0.7ms)  SELECT `contract`.`id` AS t0_r0, `contract`.`network_id` AS t0_r1, `contract`.`name` AS t0_r2, `contract`.`global_name` AS t0_r3, `contract`.`description` AS t0_r4, `contract`.`business_entity_id` AS t0_r5, `contract`.`partner_business_entity_id` AS t0_r6, `contract`.`contract_type` AS t0_r7, `contract`.`stage` AS t0_r8, `contract`.`state` AS t0_r9, `contract`.`billing_recurrence_id` AS t0_r10, `contract`.`billing_start_date` AS t0_r11, `contract`.`effective_start_date` AS t0_r12, `contract`.`effective_end_date` AS t0_r13, `contract`.`original_reference_id` AS t0_r14, `contract`.`created_at` AS t0_r15, `contract`.`updated_at` AS t0_r16, `contract`.`deleted_at` AS t0_r17, `contract`.`old_partner_business_entity_id` AS t0_r18, `contract`.`start_date` AS t0_r19, `contract`.`end_date` AS t0_r20, `relationship`.`id` AS t1_r0, `relationship`.`description` AS t1_r1, `relationship`.`network_from_id` AS t1_r2, `relationship`.`network_from_contact_id` AS t1_r3, `relationship`.`to_entity_type` AS t1_r4, `relationship`.`to_id` AS t1_r5, `relationship`.`to_contact_id` AS t1_r6, `relationship`.`relationship_type` AS t1_r7, `relationship`.`third_party_adserver_redirect_type` AS t1_r8, `relationship`.`third_party_adserver_conversion_template` AS t1_r9, `relationship`.`video_revenue_share` AS t1_r10, `relationship`.`video_revenue_flat_fee` AS t1_r11, `relationship`.`site_revenue_share` AS t1_r12, `relationship`.`site_revenue_flat_fee` AS t1_r13, `relationship`.`financial_term_notes` AS t1_r14, `relationship`.`default_bidded_ecpm` AS t1_r15, `relationship`.`active` AS t1_r16, `relationship`.`internal_id` AS t1_r17, `relationship`.`updated_at` AS t1_r18, `relationship`.`created_at` AS t1_r19, `relationship`.`distributor_cap_volume` AS t1_r20, `relationship`.`distributor_cap_period` AS t1_r21, `relationship`.`active_time` AS t1_r22, `relationship`.`role_id` AS t1_r23, `relationship`.`deleted_at` AS t1_r24 FROM `contract` LEFT OUTER JOIN `relationship` ON `relationship`.`id` = `contract`.`partner_business_entity_id` WHERE `contract`.`deleted_at` IS NULL AND (contract.name like 'ww.')
Mysql2::Error: Table 'fwrpm_oltp.relationship' doesn't exist: SELECT `contract`.`id` AS t0_r0, `contract`.`network_id` AS t0_r1, `contract`.`name` AS t0_r2, `contract`.`global_name` AS t0_r3, `contract`.`description` AS t0_r4, `contract`.`business_entity_id` AS t0_r5, `contract`.`partner_business_entity_id` AS t0_r6, `contract`.`contract_type` AS t0_r7, `contract`.`stage` AS t0_r8, `contract`.`state` AS t0_r9, `contract`.`billing_recurrence_id` AS t0_r10, `contract`.`billing_start_date` AS t0_r11, `contract`.`effective_start_date` AS t0_r12, `contract`.`effective_end_date` AS t0_r13, `contract`.`original_reference_id` AS t0_r14, `contract`.`created_at` AS t0_r15, `contract`.`updated_at` AS t0_r16, `contract`.`deleted_at` AS t0_r17, `contract`.`old_partner_business_entity_id` AS t0_r18, `contract`.`start_date` AS t0_r19, `contract`.`end_date` AS t0_r20, `relationship`.`id` AS t1_r0, `relationship`.`description` AS t1_r1, `relationship`.`network_from_id` AS t1_r2, `relationship`.`network_from_contact_id` AS t1_r3, `relationship`.`to_entity_type` AS t1_r4, `relationship`.`to_id` AS t1_r5, `relationship`.`to_contact_id` AS t1_r6, `relationship`.`relationship_type` AS t1_r7, `relationship`.`third_party_adserver_redirect_type` AS t1_r8, `relationship`.`third_party_adserver_conversion_template` AS t1_r9, `relationship`.`video_revenue_share` AS t1_r10, `relationship`.`video_revenue_flat_fee` AS t1_r11, `relationship`.`site_revenue_share` AS t1_r12, `relationship`.`site_revenue_flat_fee` AS t1_r13, `relationship`.`financial_term_notes` AS t1_r14, `relationship`.`default_bidded_ecpm` AS t1_r15, `relationship`.`active` AS t1_r16, `relationship`.`internal_id` AS t1_r17, `relationship`.`updated_at` AS t1_r18, `relationship`.`created_at` AS t1_r19, `relationship`.`distributor_cap_volume` AS t1_r20, `relationship`.`distributor_cap_period` AS t1_r21, `relationship`.`active_time` AS t1_r22, `relationship`.`role_id` AS t1_r23, `relationship`.`deleted_at` AS t1_r24 FROM `contract` LEFT OUTER JOIN `relationship` ON `relationship`.`id` = `contract`.`partner_business_entity_id` WHERE `contract`.`deleted_at` IS NULL AND (contract.name like 'ww.')
=> #<%<f66e6697Class:%<f66e6697ActiveRecord::Relation:0x007ff9e7d60888>>:0x3ffcf3eb0444>

[104] pry(main)> Contract.where("contract.name like 'w.'").includes(:partner_relationship)
  Contract Load (0.4ms)  SELECT `contract`.* FROM `contract` WHERE `contract`.`deleted_at` IS NULL AND (contract.name like 'w.')
=> []

Contract.where("contract.name like 'ww.'").includes(:partner_relationship) Contract.where("contract.name like 'w.'").includes(:partner_relationship) 确实是一样的... 不过提示 Table 'fwrpm_oltp.relationship' doesn't exist 很明显你的表那会没找到... 可能:有别人和你一起开发而 DB 版本不一样

#1 楼 @dddx Contract 和 relationship 两张表分别属于不同的数据库。

或许我没有描述清楚 查询代码不是一样的嘛 我觉得你现在可以再试试了.... 应该没问题的 1 楼

我猜想应该是因为,当你使用 includes 时,会先把 contract 查出来,再去查 relationship。第一个查询 contract 查到了数据,所以在查 relationship 时就出错了。第二个 contract 都没有数据,也就不会再查 relationship 了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号