2009-04-17 4 views
2

を "のActiveRecord :: StatementInvalid:PGError:ERRORオペレータは存在しません" 私の状態モデルは次のようになります。Postgresqlの固有のデータベースエラー:

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

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

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 

私の国のモデルは次のようになります

これは、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

エラーメッセージは非常に明確です。異なるタイプ間の比較を試みているため、 'character varying = integer'という通知があります。

解決策:country_idに 'varchar'ではなく 'int'型の列を作成します。

+0

質問と回答 – tpdi

関連する問題