No operator matches the given name and argument type(s)

╄→гoц情女王★ 提交于 2019-12-24 14:33:52

问题


I set up my Rails 3.2.x app to use PostgreSQL HStore but I'm getting an error. It looks like the hstore extension hasn't been picked up by the environment. I already rebooted my machine, checked database extensions, etc.

When I try to execute:

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

I get an error: (@> is not recognized, i.e., extension hstore is not installed?)

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

My Rails application setup is:

Gemfile.rb:

gem 'activerecord-postgres-hstore'

Migration:

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 model:

serialize :settings, ActiveRecord::Coders::Hstore

Dynamic User methods:

# 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

Update 1:

This works when I talk directly to the PostgreSQL DB:

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

The Hstore docs say:

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

So, from the look of it, my PostgreSQL database version (9.2.1) has already deprecated the => notation. It looks like I have more research ahead.


回答1:


From the PostgreSQL docs:

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

So from the look of it, my PG database version ( 9.2.1 ) has already deprecated the => notation.
Working now both locally and on Heroku.

Example:

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


来源:https://stackoverflow.com/questions/14707975/no-operator-matches-the-given-name-and-argument-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!