Search code examples
ruby-on-rails

Rake db:migrate table already exist


I don't understand why this is happening. I have the following migration:

def self.up
  create_table :leakages do |t|
    t.integer :feature_id
    t.integer :project_id
    t.float :total
    t.date :apt_date
  end
  add_index :leakages, [:feature_id, :apt_date]
end

When I run it for the first time it runs properly, but when I run the migration again then an error is thrown saying leakages table already exist. Why is this error occurring? I am using the mysql2 gem.


Solution

  • Do you have a corresponding self.down in your leakages migration? If not then you could change the method to def change, then you should be able to run rake db:migrate:redo which runs db:migrate:down then db:migrate:up for your last migration.

    The reason you are seeing that message is because the table already exists. rake db:migrate doesn't drop the table. Basically it runs the migrations that have not run yet, for the current environment.

    You might also be interested in reading: http://guides.rubyonrails.org/migrations.html#running-specific-migrations.

    So to rerun the migration(i.e. db:migrate:down then db:migrate:up) you could run:

    rake db:migrate:reset VERSION={your migration version}