2016-11-22 104 views
1

我想添加一个索引到已存在于我的表中的列中。添加索引迁移添加列而不是迁移

我想在ITEM_ID添加一个索引上product_images表

bundle exec rails generate migration AddIndexToProductImages item_id:integer:index 

,但我在迁移文件中看到的代码是

class AddIndexToProductImages < ActiveRecord::Migration 
    def change 
    add_column :product_images, :item_id, :integer 
    end 
end 

不知道这可能是造成这一点,任何人都可以帮助?谢谢。

回答

1

Rails会不会自动生成的内容迁移只为指数

编辑生成的迁移有以下几点:

class AddIndexToProductImages < ActiveRecord::Migration 
    def change 
    add_index :product_images, :item_id 
    end 
end 
+0

感谢迪帕克,已经accepeted你的答案,但有没有这样说,我可以阅读更多有关“Rails会不会自动生成的内容迁移只是指数”的任何官方文件或链接 –

1

以下是命令生成指数移民文件:

bundle exec rails generate migration AddIndexesToProductImages item_id:integer:index 

如果它没有正确的命令添加索引,则可以使用要添加的适当索引编辑生成的文件。您将不得不在change函数中执行以下操作。

def change 
    add_index :product_images, :item_id 
end