rails 3.2 migration cannot add index to create_table in change method

You can still add an index as a part of a “change” migration. You just have to do it outside of the call to create_table:

class CreateStatistics < ActiveRecord::Migration
  def change
    create_table :statistics do |t|
      t.string :name
      t.integer :item_id
      t.integer :value
      t.text :desc

      t.timestamps
    end

    add_index :statistics, [:name, :item_id]
  end
end

This correctly creates the table and then the index on an “up” migration and drops the index and then the table on a “down” migration.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)