jason328 jason328 - 1 month ago 4x
Ruby Question

Recreating a table in Rails

I'm using Rails and Postgres. I currently have a Like table created. I need to drop this table and re-create it. Here's what I've done so far.

1) Rename

migration file to
so there is no conflict with file names within migrations.

2) Change the name of the table inside the migration from
create_table :likes
create_table :likes_old

3) Generate new
migration that creates a table called Like.

Currently I'm running into the following issue when I run
rake db:migrate

PG::DuplicateTable: ERROR: relation "likes" already exists

This makes it seems like the Like table was never renamed to LikeOld. What's wrong and how do I fix this?


Old migrations don't run when you change their content, and you should not rename them. Even if they did run, changing the create_table :likes to create_table :old_likes cannot possibly change the name of an existing table. That isn't what create_table does. At best, re-running that migration will now cause a new table to be created called old_likes, with no content, while leaving your old likes table unaffected. In actuality re-running that migration will simply fail, as it will attempt to undo the migration first, dropping the table old_likes which does not yet exist.

What you need to do is create a new migration called rename_likes_to_old_likes, which actually renames the table, using rename_table. Then, either:

  • delete the old migration entirely, so you can introduce a new migration with the same name


  • create a new migration with a unique name such as create_new_likes_table or the like, and introduce your new table there.