The operator does not match the specified name and type of arguments.
I have installed a Rails 3.2.x app to use PostgreSQL HStore, but I am getting an error. It looks like the hstore extension was not selected by the environment. I've 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 the error: ( @>
not recognized, i.e. extension hstore
not installed?)
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Configuring My Rails Application:
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 methods 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
Update 1:
This works when I speak 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, due to the look and feel, my PostgreSQL database version (9.2.1) is already outdated to refer to =>
. It looks like I have more research ahead of me.
source to share
From PostgreSQL Docs :
The operator is
=>
deprecated and may be removed in a future version. Use a function insteadhstore(text, text)
.
So, due to the look and feel, my database version PG (9.2.1) is already deprecated in notation =>
.
Working now both locally and on Heroku.
Example:
User.where("settings @> hstore(?,?)", "setting_x", key)
source to share