Topic: Mysql2::Error: Can't create table (errno: 121) debugging tips

I've been struggling with this error for days, I could not find anything helpfull online, at least in my case. I resolved it via a workaround and I'd like to share what I found out.

I got this error while running rake db:migrate

I was trying to create a table named "weight_equivalents". The exact same migration code with a different table name would work.

My work around was to rename the table as "weight_translations" instead. When I went into PhpMyAdmin to then rename my table as "weight_equivalents", I got an error message saying :

#1050 - Table './recipe_development/weight_equivalents' already exists

I had deleted the table weight_equivalents before, and it was not anymore listed in my tables list for this database.

I think this is due to a bug in mysql and/or my database got corrupted.

I'd like to move further in this bug investigation: does anyone know on how to fully delete this corrupted table in order for me to run the correct migration in first place (with table name "weight_equivalents") ?

Re: Mysql2::Error: Can't create table (errno: 121) debugging tips

The most correct way to manage your tables and models is via migrations.
Occasionally you will make a mistake when writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rake db:migrate. You must rollback the migration (for example with rake db:rollback), edit your migration and then run rake db:migrate to run the corrected version.

A common task is to rollback the last migration, for example if you made a mistake in it and wish to correct it. Rather than tracking down the version number associated with the previous migration you can run:

bundle exec rake db:rollback

This will run the down method from the latest migration. If you need to undo several migrations you can provide a STEP parameter:

bundle exec rake db:rollback STEP=3

will run the down method from the last 3 migrations.

The db:migrate:redo task is a shortcut for doing a rollback and then migrating back up again. As with the db:rollback task, you can use the STEP parameter if you need to go more than one version back, for example

bundle exec rake db:migrate:redo STEP=3

If you need to run a specific migration up or down, the db:migrate:up and db:migrate:down tasks will do that. Just specify the appropriate version and the corresponding migration will have its up or down method invoked, for example,

bundle exec rake db:migrate:up VERSION=20080906120000

See more details at Rails Guides for  Migrations.