2

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.

1 Answer 1

1

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.

4
  • What would be an example of improper execution? The model responds exactly how I want in the console. Are there other non-functional (e.g. tracking) operations that can fail but still leave the functionality intact?
    – Tyler
    Commented Jul 30, 2013 at 13:58
  • An example would be that your table is created nicely, but you made a small typo in the name of a column that should be indexed (using add_index). The migration isn't finished, but because the table is there, you can't run it again to complete it without fixing it manually. Commented Jul 31, 2013 at 13:43
  • Won't it roll back the migration and undo the changes if the whole thing doesn't run?
    – Tyler
    Commented Jul 31, 2013 at 13:53
  • 1
    Not if it generates an error. rake db:migrate will tell you about the error by printing an error trail and saying "later migrations cancelled". It might seem logical that it would undo the changes and revert to its previous state, but that would be tricky because it doesn't always know what that previous state would be. Consider a migration in which you also process data. Or a migration in which you remove multiple columns. It would have to make assumptions (if it can) and that could leave you with much worse results. Commented Aug 1, 2013 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.