【问题标题】:How would I add an index on a key in a hstore column?如何在 hstore 列中的键上添加索引?
【发布时间】:2013-10-16 16:53:34
【问题描述】:

关注http://railscasts.com/episodes/345-hstore

我决定实现一些 hstore 列。

据我了解

execute "CREATE INDEX products_gin_properties ON products USING GIN(properties)"

或者更好地写成(Rails 4):

add_index :products, :properties, using: :gin

两者都只在 hstore 列上创建索引。

如何在 hstore 列中的键上添加索引?环顾四周,我可以做类似的事情:

execute "CREATE INDEX products_properties_name ON products (properties -> 'name')"

但是,是否有 Rails 4 方法可以做到这一点?

【问题讨论】:

    标签: ruby-on-rails postgresql ruby-on-rails-4 hstore


    【解决方案1】:

    只看这里的源代码:https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L418

        def add_index(table_name, column_name, options = {}) #:nodoc:
          index_name, index_type, index_columns, index_options, index_algorithm, index_using = add_index_options(table_name, column_name, options)
          execute "CREATE #{index_type} INDEX #{index_algorithm} #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} #{index_using} (#{index_columns})#{index_options}"
        end
    

    index_columns 是一个逗号分隔的列列表。

    好像不支持。

    【讨论】:

      猜你喜欢
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多