Mathieu, in such situations where you are not sure that something is being created or not being created: it is best to just create what you think is necessary (in this case, an index) and see what happens when the migration starts.
The logic behind this is that if your :deal_id column :deal_id already indexed and your migration tries to reindex it, it will receive an error message and the migration will be canceled so you can fix it. However, if you did not add the index to your migration, you obviously will not get any errors, but you will need to take an additional step to check if the index exists.
class CreateDeal < ActiveRecord::Migration def change create_table :deals do |t| t.string :name t.timestamps end create_table :products do |t| t.belongs_to :Deal t.timestamps end add_index :products, :deal_id end end
Note that you also want to add the index after the table creation process is complete. Using the add_index helper inside the create_table helper is likely to result in an error.
source share