2009-04-17 8 views
2

Mein Zustand Modell wie folgt aussieht:Postgresql spezifische Datenbankfehler: "Active :: StatementInvalid: PGError: ERROR: Operator existiert nicht"

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 

Mein Land Modell wie folgt aussieht:

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

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

Dies funktioniert in einer SQLite-Umgebung:

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

Aber in einer postgresql Umgebung, bekomme ich diese:

>> 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) 

Irgendwelche Gedanken?

Antwort

8

Die Fehlermeldung ist ziemlich klar, Sie versuchen, einen Vergleich zwischen verschiedenen Typen zu machen, daher die 'character variating = integer' Nachricht.

Die Lösung: make country_id eine Spalte vom Typ 'int' anstelle von 'varchar'.

+0

Gefragt und beantwortet. – tpdi

Verwandte Themen