ERROR 1005: Can't create table (errno: 121)
errno 121 means a duplicate key error. Probably the table already exists in the InnoDB internal data dictionary, though the .frm file for the table has been deleted. This is the most common reason for getting errno 121 in table creation. Another possible reason is a name conflict in a foreign key constraint name. Constraint names must be unique in a database, like table names are.
意思是:
1、表名重复
2、以该名字命名的表之前创建过后来删除了,但是对应的.frm文件还留在磁盘上
3、主键名字在全数据库范围内不是唯一的
以上是在网上找到的错误相关信息。
结合今天折腾半天找到的问题根源,总结如下:
首先,排查外键是否是全局唯一
使用如下命令,将$database 替换为要查询的数据库
select
concat(table_name, '.', column_name) as 'foreign key',
concat(referenced_table_name, '.', referenced_column_name) as 'references'
from
information_schema.key_column_usage
where
table_schema='$database' and
referenced_table_name is not null;
则进行第二步查询,是否是约束重名
SELECT
constraint_name,
table_name
FROM
information_schema.table_constraints
WHERE
constraint_type = 'FOREIGN KEY'
AND table_schema = DATABASE()
ORDER BY
constraint_name;
经过以上两步,基本可以发现是否是全局命名冲突引起的建表失败。
我的错误就是全局范围内出现约束重名。
好坑啊