添加索引迁移添加列而不是迁移

add index migration adding column instead of migration

我正在尝试将索引添加到我的 table 中已经存在的列。

我想在 item_id product_images table

上添加索引
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

不确定是什么原因造成的,有人可以帮忙吗?谢谢

Rails 不会自动生成仅包含索引

内容的迁移

使用以下内容编辑生成的迁移:

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

生成索引迁移文件的命令如下:

bundle exec rails generate migration AddIndexesToProductImages item_id:integer:index

如果没有添加索引的正确命令,您可以使用要添加的适当索引编辑生成的文件。您必须在 change 函数中输入以下内容。

def change
  add_index :product_images, :item_id
end