【问题标题】:No operator matches the given name and argument type(s)没有运算符与给定名称和参数类型匹配
【发布时间】:2026-01-10 15:35:01
【问题描述】:

我将 Rails 3.2.x 应用程序设置为使用 PostgreSQL HStore,但出现错误。 看起来 hstore 扩展尚未被环境拾取。 我已经重启了我的机器,检查了数据库扩展等。

当我尝试执行时:

User.where("settings @> (:key => :value)", :key => "setting_x", :value => "test")

我收到一个错误:(@> 无法识别,即扩展名hstore 未安装?)

HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

我的 Rails 应用程序设置是:

Gemfile.rb:

gem 'activerecord-postgres-hstore'

迁移:

add_column :users, :settings, :hstore
execute "CREATE INDEX CONCURRENTLY users_gin_settings ON users USING GIN(settings)"
# can see the extenstion installed on my local dev psql database after this

User模特:

serialize :settings, ActiveRecord::Coders::Hstore

动态User 方法:

# metaprogramming: has_setting_x + instance.setting_x
%w[setting_x setting_y setting_z].each do |key|
attr_accessible key
    # doesn't work > throws error because of the @> operator
    scope "has_#{key}", lambda { |value| where("settings @> (? => ?)", key, value) }

    # works: can use instance.setting_x
    define_method(key) do
      settings && settings[key]
    end

    # works: can use instance.setting_x = "value"
    define_method("#{key}=") do |value|
      self.settings = (settings || {}).merge(key => value)
    end
end

更新 1:

这在我直接与 PostgreSQL DB 对话时有效:

SELECT "users".* FROM "users" WHERE (settings @> hstore('setting_x','6D9Q7RO4SVWHXK86F'));

Hstore docs 说:

The => operator is deprecated and may be removed in a future release. Use the hstore(text, text) function instead.

所以,从表面上看,我的 PostgreSQL 数据库版本 (9.2.1) 已经弃用了 => 表示法。看来我还有更多的研究要做。

【问题讨论】:

  • 不要在您的问题中添加“解决方案:”。添加一个答案,当 Stack Overflow 超时时,你可以接受你的答案。
  • @Sean 是的,也尝试过这种表示法。
  • +1 编辑和问题现在好多了。 Stack Overflow 喜欢*式的文章,所以要写清楚、简洁、完整的句子。您会注意到我的编辑删除了一些缩写并改进了语法。

标签: ruby-on-rails ruby heroku postgresql-9.1 hstore


【解决方案1】:

来自PostgreSQL docs

=> 运算符已弃用,可能会在未来的版本中删除。请改用hstore(text, text) 函数。

所以从外观上看,我的 PG 数据库版本 ( 9.2.1 ) 已经弃用了 => 表示法。
现在在本地和 Heroku 上工作。

例子:

User.where("settings @> hstore(?,?)", "setting_x", key)

【讨论】:

    最近更新 更多