Rails and Postgres Hstore: Can you add an index in a migration?

早过忘川 提交于 2019-12-03 12:24:40

Yes! You can make another migration and use the 'execute' method... like so:

class IndexProductsGinData < ActiveRecord::Migration
  def up
    execute "CREATE INDEX products_gin_data ON products USING GIN(data)"
  end

  def down
    execute "DROP INDEX products_gin_data"
  end
end

UPDATE: You might also want to specify this line in config/application.rb:

config.active_record.schema_format = :sql

You can read about it here: http://apidock.com/rails/ActiveRecord/Base/schema_format/class

In Rails 4, you can now do something like this in a migration:

    add_index :products, :data, using: :gin
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!