问题
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 thehstore(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