2009-04-17 23 views
2

我的狀態模型看起來是這樣的:PostgreSQL特有的數據庫錯誤: 「的ActiveRecord :: StatementInvalid:PGError:ERROR:運營商不存在」

class State < ActiveRecord::Base 
    belongs_to :country 
    named_scope :order_by_name, :order => :name 

    validates_presence_of [:country, :name] 

    def <=>(other) 
    name <=> other.name 
    end 

end 

我國模型是這樣的:

class Country < ActiveRecord::Base 
    has_many :states 
    named_scope :order_by_name, :order => :name 

    def <=>(other) 
    name <=> other.name 
    end 
end 

這個工作在SQLite的環境:

>> Country.find_by_iso("US").states 
=> [#<State id: 1, name: "Alaska", abbr: "AK", # ..... 

但在PostgreSQL的環境中,我得到這個:

>> Country.find_by_iso("US").states 
ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist: character varying = integer 
LINE 1: SELECT * FROM "states" WHERE ("states".country_id = 214) 
                 ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
: SELECT * FROM "states" WHERE ("states".country_id = 214) 

有什麼想法?

回答

8

錯誤消息非常明顯,您試圖在不同類型之間進行比較,因此「字符變化=整數」通知。

解決方案:使country_id成爲'int'類型的列而不是'varchar'。

+0

提問並回答。 – tpdi 2009-04-17 07:22:18