【问题标题】:Postgresql-specific database error: "ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist"Postgresql 特定的数据库错误:“ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist”
【发布时间】:2010-10-20 00:56:00
【问题描述】:

我的状态模型如下所示:

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) 

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails postgresql


    【解决方案1】:

    错误信息很清楚,您正在尝试在不同类型之间进行比较,因此会出现“字符变化 = 整数”通知。

    解决方案:将 country_id 设为 'int' 类型的列,而不是 'varchar'。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-10
      • 2023-03-13
      • 2016-10-25
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 2021-10-09
      • 2021-10-01
      相关资源
      最近更新 更多