Question

MySQL is giving me grief when I try to recreate a particular table in a particular database; I believe the problem is that somewhere MySQL has some records of the table's foreign keys and keeps trying to check the constraint when I try to recreate the table.

The database in question is for the test environment and is periodically refreshed by dumping the prod DB, dropping the test DB, creating the test DB again and then applying the prod DB dump to the new test DB.

However last night when I went to do this procedure I suddenly ran into MySQL's errno: 121, which is normally related to an incorrect foreign key... If I try with a different table defnition, without specifying a key, I get errno: 150 instead. However, this only occurs if I try to apply the dump to a new database of exactly the same name as the old one. If I create a database with a different name from the old one (on the same server still) and apply the dump, there is no problem.

In fact, I can't even create a table of exactly the same name without any keys at all even in a completely new database.

This example demonstrates the problem

mysql> create database test_db;
Query OK, 1 row affected (0.01 sec)

mysql> connect test_db;
Connection id:    1590
Current database: test_db

mysql> create table client_feed_group(id int);
ERROR 1005 (HY000): Can't create table './test_db/client_feed_group.frm' (errno: 150)
mysql> create database new_db_name;
Query OK, 1 row affected (0.00 sec)

mysql> connect new_db_name;
Connection id:    1591
Current database: new_db_name

mysql> create table client_feed_group(id int);
Query OK, 0 rows affected (0.01 sec)

I have already dropped the test_db database; this should remove all table and all records. I then create it, connect to it and try to create the old table client_feed_group; I get an error. I create another database with a different name and follow the same steps; this time there is no problem.

Was it helpful?

Solution

It appears to be the same as this MySQL bug.

The procedure located far down in the comments worked for me, that is

  • create the table again using the same schema as the old one, but using a MyISAM engine
  • use ALTER TABLE to change the engine to innodb

thereafter things appear to work correctly; I can now drop the database and do a full restore with no problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top