4

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)

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • It works like you expect. Did you get any other error when you first ran that first migration? Somehow the migration ran without being marked as ran in the database. That's not supposed to happen. – Alex Wayne Aug 20 '13 at 01:24
  • I did not get any other error, but I've been messing around learning this and although I was trying to keep perfect track of things I might have missed something and caused this inadvertently. Is the only solution to delete the models and start over? – temporary_user_name Aug 20 '13 at 01:25
  • Like Alex mentioned this shouldn't happen. Can you `grep "create_table :users" db/migrate/*` to make sure there aren't two accidental create table migrations, just as a sanity check? – kikuchiyo Aug 20 '13 at 01:27
  • Try dropping your database (`rake db:drop`), then recreating your database (`rake db:create`) before trying to re-run your migrations, there is a chance that your migrations are out of sync with your database – Dan McClain Aug 20 '13 at 01:27
  • I just walked into a nightmare maze of other problems...can't run `db:create` because privileges are insufficient...time to go @$%@ with postgres for three days. – temporary_user_name Aug 20 '13 at 01:37
  • Oh poor 2013 me. I wish I could go hold his hand. – temporary_user_name Apr 24 '19 at 15:31

3 Answers3

3

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

Rodrigo Zurek
  • 4,555
  • 7
  • 33
  • 45
0

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

techvineet
  • 5,041
  • 2
  • 30
  • 28
0

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.

ricks
  • 3,154
  • 31
  • 51