2

I am trying to add a index to a column which already exists in my table by.

I want to add a index on item_id on product_images table

bundle exec rails generate migration AddIndexToProductImages item_id:integer:index

but the code i see in the migration file is

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

Not sure what could be causing this, can anyone help? Thanks.

2 Answers 2

2

Rails will not autogenerate the migration with content just for index

Edit the generated migration with following:

class AddIndexToProductImages < ActiveRecord::Migration
  def change
    add_index :product_images, :item_id
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks deepak, have accepeted your answer but is there any official documentation that says so or link where i can read more about "Rails will not autogenerate the migration with content just for index"
1

Following is the command to generate index migration File:

bundle exec rails generate migration AddIndexesToProductImages item_id:integer:index

In case it does not have proper command to add index, you can edit the generated file with the appropriate index you want to add. You will have to wring following in the change function.

def change
  add_index :product_images, :item_id
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.