2015-05-04 32 views
0

我正嘗試在我正在製作的一個小爬蟲上使用String作爲主鍵/外鍵。不過,我不斷收到以下錯誤,當我嘗試使用關聯的方法(例如:a.crawler_details - 當一個是叫莘對象):在Rails上使用字符串作爲PK和FK 4

RangeError: 8532503039 is out of range for ActiveRecord::Type::Integer with limit 4 

病案舉例:

#<Asin asin: "8532503039", title: "O Físico", image_url: nil, active: false, created_at: "2015-05-04 03:30:29", updated_at: "2015-05-04 03:30:36"> 

這裏是的細節:

2.1.2 :001 > Asin.new 
=> #<Asin asin: nil, title: nil, image_url: nil, active: true, created_at: nil, updated_at: nil> 

2.1.2 :002 > CrawlerDetail.new 
=> #<CrawlerDetail id: nil, amazon_price: nil, feed_price: nil, first_place: nil, second_place: nil, third_place: nil, fp_price: nil, sp_price: nil, tp_price: nil, run: nil, created_at: nil, updated_at: nil> 


class Asin < ActiveRecord::Base 
    has_many :crawler_details, :foreign_key => 'id', :primary_key=> 'asin' 

    self.primary_key = 'asin' 
    ... 
end 


class CrawlerDetail < ActiveRecord::Base 
    has_one :asin, :foreign_key => 'asin', :primary_key => 'id' 
end 

我也嘗試了belongs_to關係,但沒有運氣。這裏的任何想法?

回答

0

我認爲這不是關聯錯誤,您是否在數據庫表中定義了字段名稱「asin」類型整數

它可能會幫助請嘗試將您的數據庫表從整數類型更改爲字符串。