mysql - 无法添加外键约束 - 无法解析接近错误的表名
问题描述
尝试在我的 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_id
and之间创建一个外键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
)引用gds
(id
):无法解析接近的表名:(id
)
老实说,我无法理解这个问题。该表gds
存在(我可以运行select * from gds
)。我的命令有语法问题吗?
解决方案
推荐阅读
- installation - 终端:“找不到命令”
- java - 使用 BindingResult 进行 Spring MVC 集成测试
- sql-server - 如何在 ROW_NUMBER() TSQL 中使用过滤
- php - 执行 CURL 请求时出错:资源 ID #8
- javascript - ChartJS - 根据悬停数据显示百分比(AngularJS)
- vuejs2 - 如何让手动添加的 Vue 组件出现在 Vue 开发工具中?
- python - 泊松过程的困惑
- javascript - 显示JSON的所有数据
- docusignapi - 从 GET DOCUMENT REST API 调用 Docusign 保存“PDF 文件”输出
- c# - 如何在 Uwp 中动态更改 RadialGradientBrush 的渐变颜色