Search code examples
ruby-on-railspostgresqlactiverecorddatabase-migration

Rails pending migration in rake db:test:prepare


I've run rake db:migrate and all of my migrations ran. However, when I try to run rake db:test:prepare I get the error:

You have 1 pending migrations:
  20130724211328 CreateGalleries
Run `rake db:migrate` to update your database then try again.

Then running rake db:migrate again gives the error:

PG::Error: ERROR:  relation "galleries" already exists...

But in the console I can create and manipulate the Gallery model exactly as shown in the CreateGalleries migration. The table is not being created or even mentioned in any other migrations.

It seems the migration ran just fine but did not register. Any ideas how to fix this?

EDIT

I solved this with rake db:drop db:create db:migrate then rake db:test:prepare, but I'm happy to give the solution to anyone who can shed light on what caused the problem in the first place.


Solution

  • I suspect the migration for galleries hasn't been executed properly. If you're 100% sure everything is right in your table, you can bump up the migration version to the version of the galleries migration.

    To do this, find the timestamp of your galleries migration (the 14 numbers in front of your migration file, in this case 20130724211328) and insert this as a new row into the table schema_migrations (which is done automatically by Rails after successfully executing a migration).

    If the table is empty, you could also drop the table galleries and run rake db:migrate again. This way you can also make sure your migration doesn't trigger any errors.