temporary_user_name
temporary_user_name

Reputation: 37018

Rake db:migrate not ignoring old migrations?

Running through Michael Hartl's well known Rails tutorial, hit this snag.

I have this in a migration file, created by rails generate model etc:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email

      t.timestamps
    end
  end
end

Later, I added this second migration file:

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :email, unique: true
  end
end

To try and update the database to include the new one, I followed the instructions and ran rake db:migrate, but this gives me an error telling me I'm trying to create a table that already exists, which is to say I'm clearly missing something.

Am I...supposed to delete the first migration? That wouldn't make any sense. What to do?

(These are the only files under db/migrate)

Upvotes: 4

Views: 2508

Answers (3)

ricks
ricks

Reputation: 3324

My issues was my 2nd to last migration ran and it was trying to rerun that migration again after i added a new migration.

I ended up just commenting out the code inside the one it was trying to rerun, and then ran rake db:migrate then uncommented the migration code.

This way the schema does not break and it fixes whatever bug occurred.

Upvotes: 0

techvineet
techvineet

Reputation: 5111

What you can do is rollback couple of migration and re-run.

You can rollback migration like this

#rake db:rollback STEP=2

and then run

#rake db:migrate

Hope it should work

Upvotes: 0

Rodrigo Zurek
Rodrigo Zurek

Reputation: 4575

if you realy want to see what migrations have been ran into the database, you can inspect your app database, there is a table called schema_migrations, in there you can see the unique id of each migration as a row for example is your migration is called: 20130402190449_add_flagand_table.rb, you should see the number 20130402190449 as a row of that table, hope i gave you some guidance

Upvotes: 3

Related Questions