Search code examples
ruby-on-railsrollbackrails-migrations

migration not rolling back


I had run this migration:

class AddUniqueToLocationColumnName < ActiveRecord::Migration
  def change
    remove_index :locations, :name
    add_index :locations, :name, unique: true
  end
end

And now I am trying to rollback but its showing error:

StandardError: An error has occurred, this and all later migrations canceled: remove_index is only reversible if given a :column option.

how can I roll back this migration to my previous version?


Solution

  • Try explicitly define up and down:

    class AddUniqueToLocationColumnName < ActiveRecord::Migration
      def self.up
        remove_index :locations, column: :name
        add_index :locations, :name, unique: true
      end
    
      def self.down
        remove_index :locations, column: :name # remove unique index
        add_index :locations, :name # adds just index, without unique
      end
    end