0

私は既存の MSSQL データベースを使用していますが、これは他の人によって構築されたレガシー アプリケーションであるため、ほとんどまたはまったく制御できません。1 対多の関連付けで 2 つのテーブルを関連付けようとしています。

関連付けを定義するコード スニペットは次のとおりです。

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。実際、それは私が今行っていることです... しかし、関連付けが機能しない理由を理解したいと思います.

4

1 に答える 1

0

生成された SQL (SQL を表示するために Sequel にデータベース ロガーを追加) やスキーマに関する詳細を投稿しなかったため、rollhst テーブルと rollinventory テーブルの主キーの値は文字列ではなく整数であると推測できます。おそらく、参照する関連付けに :primary_key オプションを設定する必要がありroll_numます:primary_key=>:roll_num

于 2014-06-06T15:42:15.183 に答える