没有运算符匹配给定的名称和参数类型

时间:2013-02-05 12:49:31

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

我设置了我的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.

My 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)已经弃用了=>表示法。看起来我还有更多的研究。

1 个答案:

答案 0 :(得分:7)

来自PostgreSQL docs

  

{@ 1}}运算符已弃用,可能会在将来的版本中删除。请改用=>功能。

因此,从我看来,我的PG数据库版本(9.2.1)已经弃用了hstore(text, text)符号。
现在在本地和Heroku工作。

示例:

=>