繁体   English   中英

Postgresql特定的数据库错误:“ActiveRecord :: StatementInvalid:PGError:ERROR:运算符不存在”

[英]Postgresql-specific database error: “ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist”

我的州模型看起来像这样:

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) 

有什么想法吗?

错误信息非常清楚,您正在尝试对不同类型进行比较,因此“字符变化=整数”通知。

解决方案:使country_id成为'int'类型的列而不是'varchar'。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM