1

I recently started using Rails, and created a few Models using the CLI which in turn created some migrations.

I ran the rake db:migrate command after adding all my columns in there, and then realized that I'd left out the associations.

So what did I do? I went ahead and edited the migrations to include those keys.

I ran rake db:migrate again, and nothing changed in the schema.

Then I ran rake db:reset and then rake db:setup.

When that didn't work, I deleted my schema.rb (the darn thing wouldn't get updated!) and tried recreating it. When I realized that didn't work, I dropped the database, and killed the schema.

Now I'm stuck with some manually modified migrations, no schema.rb and no database.

How do I get the modified migrations to generate a schema, and play nice with Rails?

Community
  • 1
  • 1
FloatingRock
  • 6,741
  • 6
  • 42
  • 75

1 Answers1

4

In development it does not matter to drop and rebuild your database. I do it often and I even have a rake task for that. The 3 command to chain are:

rake db:drop
rake db:create
rake db:migrate
# And a 4rth optional command to rebuild your test database
rake db:test:prepare

With this you should be good

Next time you need to modify a migration manually after migrating it, you should process by:

  1. rake db:rollback
  2. edit your migration
  3. rake db:migrate

Following those steps will save you some headaches


Bonus info:

After you deployed your migration to your production server you cannot manually modify it, hence you must write another migration that will perform the modification (adding columns, etc...)

Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73