我正在使用我几乎无法控制的现有MSSQL数据库,因为它是由其他人构建的遗留应用程序。我试图在one_to_many关联中关联两个表。
以下是定义关联的代码片段:
class RollHistory < Sequel::Model :rollhst
many_to_one :roll_inventory, :key => :roll_num
end
class RollInventory < Sequel::Model :rollinventory
one_to_many :roll_history, :key => :roll_num
end
注意:这两个类在没有关联的情况下按预期工作......
pry(main)> DB.roll_history.where(:roll_num => 'ZB30217').first
=> #<RollHistory @values={:seqnum=>1054318, :status=>"D", :del_date=>"20081120", :date_rcvd=>"20081120", :inv_num=>"CG868489", :line_num=>1, :roll_num=>"ZB30217", :job_name=>"CHATEAU MONTAGNE", :cust_name=>"CHATEAU MONTAGNE", :amt_used=>92.5, :beg_length=>123.25, :custlineseqnum=>0, :deltktdate=>" ", :remarks=>"", :trans_date=>"20111027", :grs_cost=>1.75, :adjustmentid=>0}>
pry(main)> DB.roll_inventory.where(:roll_num => 'ZB30217').first
=> #<RollInventory @values={:pr_code=>"01", :manf=>"SPECIAL ORDER", :supplier=>"SHAW INDUSTRIES, INC", :style_num=>"50249", :style=>"WINCHESTER 26 OZ", :colornum=>"", :color=>"YORK BLUE", :backing=>0, :notused_1=>" ", :roll_num=>"ZB30217", :dye_lot=>"80721", :quality=>1, :width=>12.0, :beg_length=>123.25, :amt_used=>97.25, :amt_resv=>0.0, :amt_avail=>26.0, :grs_cost=>0.5, :net_cost=>0.5, :freight=>0.0, :frt_code=>"", :inv_date=>"20081113", :sidemark=>"DS/CHATEAU MONTAGNE", :date_rcvd=>"20081113", :inv_num=>"2210643", :roll_cut=>"R", :fibertype=>2, :styletype=>0, :notused_2=>" ", :location=>"", :privlabelco=>"SHAW INDUSTRIES, INC", :ponumber=>"#ST175780003", :softreserve=>0, :unit_price=>0.0, :colortype=>0, :weight=>0.0, :pile=>0.0, :toxicitynum=>"", :load=>0.0, :comments=>"REF.#365109", :store=>32, :seqnum=>4646, :stock=>0, :errm=>0, :serialno=>"", :pricelistseqnum=>0, :colorseqnum=>0, :source=>" ", :ap_voided=>0, :userreal1=>0.0, :insertedby=>"Not Known", :inserteddatetime=>nil, :lastchangedby=>"WS101:jpereira", :lastchangeddatetime=>2012-10-17 13:00:08 -0400, :trans_date=>nil, :useextendedhistory=>0, :collection=>"", :privcollection=>""}>
但是,这是我尝试使用关联时得到的输出:
pry(main)> DB.roll_history.where(:roll_num => 'ZB30217').first.roll_inventory
Sequel::DatabaseError: TinyTds::Error: Conversion failed when converting the nvarchar value 'ZB30217' to data type int.
from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/sequel-4.7.0/lib/sequel/adapters/tinytds.rb:233:in `fields'
pry(main)> DB.roll_inventory.where(:roll_num => 'ZB30217').first.roll_history
Sequel::DatabaseError: TinyTds::Error: Conversion failed when converting the varchar value '#10665277' to data type int.
from /usr/local/var/rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/sequel-4.7.0/lib/sequel/adapters/tinytds.rb:233:in `fields'
我不确定#10665277
表示的是什么,因为无论我使用哪roll_num
,我得到相同的数字,所以我怀疑这是解决这个问题的关键,但是现在我正处于损失。
在定义关联时我是否做错了,因为键是字母数字?我在SEQUEL文档中找不到任何关于此的内容。
显然,我可以简单地从roll_history
收集所有roll_num
的记录,实际上,这就是我现在正在做的事情......但是我想知道为什么协会不起作用。
答案 0 :(得分:0)
您没有发布生成的SQL(在Sequel中添加数据库记录器以查看SQL)或有关架构的任何详细信息,因此我只能猜测rollhst和rollinventory表的主键值是整数而不是字符串。您可能需要在关联上设置:primary_key选项以引用roll_num
::primary_key=>:roll_num
。