问题描述
我有一个像这样创建产品表的迁移
I have a migration where I create a products table like so
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.hstore :data
t.timestamps
end
end
end
在 activerecord-postgres-hstore 页面上,他们将索引添加到表 (在 SQL 中)与
On the activerecord-postgres-hstore page they add an index to the table (in SQL) with
CREATE INDEX products_gin_data ON products USING GIN(data);
但是迁移不会跟踪这种变化(我猜是因为它是 Postgres 特有的?),有没有办法从迁移中创建索引?
However that change is not tracked by migrations (I'm guessing because it's Postgres specific?), is there a way to create an index from within a migration?
谢谢!
推荐答案
是的!您可以进行另一次迁移并使用执行"方法...像这样:
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
更新:您可能还想在 config/application.rb 中指定这一行:
UPDATE: You might also want to specify this line in config/application.rb:
config.active_record.schema_format = :sql
您可以在这里阅读:http://apidock.com/rails/ActiveRecord/Base/schema_format/类
这篇关于Rails 和 Postgres Hstore:可以在迁移中添加索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!