没有运算符匹配给定的名称和参数类型 [英] No operator matches the given name and argument type(s)
问题描述
我将Rails 3.2.x应用程序设置为使用PostgreSQL HStore,但出现错误。
看来环境尚未使用hstore扩展程序。
我已经重新启动计算机,检查数据库扩展等。
当我尝试执行时:
User.where( settings @>(:key =>:value),:key => setting_x,:value => test)
我收到一个错误:( @>
无法识别,即未安装扩展名 hstore
?
提示:没有运算符匹配给定的名称和参数类型。您可能需要添加显式类型转换。
我的Rails应用程序设置为:
Gemfile.rb:
gem'activerecord-postgres-hstore'
迁移:
add_column:users,:settings,:hstore
$之后可以看到安装在本地dev psql数据库上的扩展p $ p>
执行使用GIN(设置)在用户上同时创建用户的索引
#在此
用户
型号:serialize:settings,ActiveRecord :: Coders :: Hstore
动态
User
方法:#元编程:has_setting_x + instance.setting_x
%w [setting_x setting_y setting_z]。每个| key |
attr_accessible键
#不起作用>由于@>引发错误运算子
范围 has _#{key},lambda {|值| where( settings @>(?=>?),键,值)}
#可以工作:可以使用instance.setting_x
define_method(key)做
设置&& settings [key]
end
#可以工作:可以使用instance.setting_x = value
define_method(#{key} =)do | value |
self.settings =(设置|| {})。merge(key => value)
结束
结束
更新1:
当我直接与PostgreSQL数据库:
选择用户。*从用户位置(设置@> hstore('setting_x',' 6D9Q7RO4SVWHXK86F'));
Hstore文档说:
==>不推荐使用该运算符,并且可能在将来的版本中将其删除。请改用hstore(text,text)函数。
因此,从它的外观来看,我的PostgreSQL数据库版本(9.2.1)已经弃用了
=>
表示法。看来我还有很多研究要做。解决方案来自 PostgreSQL文档:
=>
运算符已弃用,在以后的版本中可能会删除。请使用hstore(text,text)
函数。
看起来,我的PG数据库版本(9.2.1)已弃用
=>
表示法。
现在在本地和Heroku上均可使用。
示例:
User.where( settings @> hstore(?,?), setting_x,键)
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., extensionhstore
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.解决方案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)
这篇关于没有运算符匹配给定的名称和参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!