1

我将 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.

我的 Rails 应用程序设置是:

宝石文件.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文档说:

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

所以,从表面上看,我的 PostgreSQL 数据库版本(9.2.1)已经弃用了这个=>符号。看来我还有更多的研究要做。

4

1 回答 1

7

来自PostgreSQL 文档

=>运算符已弃用,可能会在将来的版本中删除。请改用该hstore(text, text)功能。

所以从外观上看,我的 PG 数据库版本( 9.2.1 )已经弃用了这个=>符号。
现在在本地和 Heroku 上工作。

例子:

User.where("settings @> hstore(?,?)", "setting_x", key)
于 2013-02-05T15:50:13.997 回答