1215-Cannot add foreign key constraint
2017-07-05 12:59
465 查看
1、错误描写叙述
![](http://img.blog.csdn.net/20150824095139693?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150824095936721?<br/><br/>watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2、错误原因
昨天我对一张表的字段加入外键。保存时一直提示不能加入外键约束
3、解决的方法
(1)外键相应的字段数据类型不一致
(2)两张表的存储引擎不一致
(3)设置外键时“删除时”设置为“SET NULL”
于是,我利用排除法,首先查看表的存储引擎,发现都是InnoDB引擎。排除第二条;设置外键时“删除时”设置为“SET NULL”,我改为其它的选项,发现也不能保存。故排除了第三项;接着,我查看了外键相应的字段的数据类型。发现它们居然不一致
t_stu_info中的id字段是bigint(8),而t_tea_info中的t_stu字段是varchar(20) ,将后者也改为bigint(8),能够成功地保存
故这次错误应该是外键相应的字段的数据类型不一致
2、错误原因
昨天我对一张表的字段加入外键。保存时一直提示不能加入外键约束
3、解决的方法
(1)外键相应的字段数据类型不一致
(2)两张表的存储引擎不一致
(3)设置外键时“删除时”设置为“SET NULL”
于是,我利用排除法,首先查看表的存储引擎,发现都是InnoDB引擎。排除第二条;设置外键时“删除时”设置为“SET NULL”,我改为其它的选项,发现也不能保存。故排除了第三项;接着,我查看了外键相应的字段的数据类型。发现它们居然不一致
t_stu_info中的id字段是bigint(8),而t_tea_info中的t_stu字段是varchar(20) ,将后者也改为bigint(8),能够成功地保存
故这次错误应该是外键相应的字段的数据类型不一致
相关文章推荐
- MySQL添加外键时报错:1215 Cannot add the foreign key constraint的解决方法
- MySQL添加外键时报错:1215 Cannot add the foreign key constraint的解决方法
- MySQL中创建外键的错误:1215 Cannot add the foreign key constraint
- 异常处理:1215 - Cannot add foreign key constraint
- 1215 - Cannot add foreign key constraint
- [Err] 1215 - Cannot add foreign key constraint
- ERROR 1215 (HY000): Cannot add foreign key constraint
- 1215-Cannot add foreign key constraint
- 1215 Cannot add the foreign key constraint
- 错误代码: 1215 Cannot add foreign key constraint
- 1215 - Cannot add foreign key constraint
- ERROR 1215 (HY000): Cannot add foreign key constraint
- 1215 - Cannot add foreign key constraint
- mysql异常 [Err] 1215 - Cannot add foreign key constraint
- 1215 - Cannot add foreign key constraint
- 1215 - Cannot add foreign key constraint
- ERROR 1215 (HY000): Cannot add foreign key constraint 解决方法
- 1215-Cannot add the foreign key constraint
- MySQL 1215 Cannot add foreign key constraint 错误解决办法
- Laravel 5.5 迁移报错:General error: 1215 Cannot add foreign key constraint