首页 > 解决方案 > 无法添加外键约束 - 无法解析接近错误的表名

问题描述

尝试在我的 MySQL 数据库中创建外键约束时出错。

注意:我的问题可能看起来非常类似于:MySQL Foreign Key - Cannot Resolve Table Name Close TO。但是,这个问题的解决方案不适合我的问题,因为我的表名和列名没有交换。

MySQL版本:5.7

我有一张桌子,叫做gds

DESCRIBE gds;
+--------------------+----------------------+------+-----+---------+----------------+
| Field              | Type                 | Null | Key | Default | Extra          |
+--------------------+----------------------+------+-----+---------+----------------+
| id                 | int(10) unsigned     | NO   | PRI | NULL    | auto_increment |
| created_at         | timestamp            | YES  |     | NULL    |                |
| updated_at         | timestamp            | YES  |     | NULL    |                |
| name               | varchar(255)         | NO   |     | NULL    |                |
| logo               | varchar(255)         | NO   |     |         |                |
+--------------------+----------------------+------+-----+---------+----------------+

我创建了一个新表,名为super_valuations

DESCRIBE super_valuations;
+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| id           | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| created_at   | timestamp        | YES  |     | NULL    |                |
| updated_at   | timestamp        | YES  |     | NULL    |                |
| gds_id       | int(10) unsigned | YES  |     | NULL    |                |
| name         | varchar(255)     | NO   |     |         |                |
+--------------+------------------+------+-----+---------+----------------+

现在,我想在super_valuations.gds_idand之间创建一个外键gds.id

我尝试了以下命令:

ALTER TABLE `super_valuations` 
ADD CONSTRAINT `super_valuations_gds_id_foreign` 
FOREIGN KEY (`gds_id`) 
REFERENCES `gds` ( `id` );

这会导致错误:

ERROR 1215 (HY000): 无法添加外键约束

我尝试运行SHOW ENGINE INNODB STATUS;,该LATEST FOREIGN KEY ERROR部分返回:

最新的外键错误

2019-06-10 10:02:53 0x7f93a0564700 表 dbname/#sql-1_401 的外键约束错误:外键(gds_id)引用gdsid):无法解析接近的表名:(id

老实说,我无法理解这个问题。该表gds 存在(我可以运行select * from gds)。我的命令有语法问题吗?

标签: mysqlsqlforeign-keys

解决方案


推荐阅读