创建外键时报 Cannot add foreign key constraint 解决方法
一开始的表结构如下:![mysql创建外键时报 SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint错误的解决方法](https://cdn.learnku.com/uploads/images/201910/21/23301/JGO9BkSsu4.png!large)
当我执行 php artisan migrate的时候报错:
1:SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table store_menus add constraint store_menus_parent_i d_foreign foreign key (parent_id) references store_menus (id) on delete cascade)
2:SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
在网上查找了资料说是外键关联的数据结构不对,查看了表的数据结构发现ID主键自增是int unsigned类型的,而我关联的parent_id是int类型的,所以数据结构不一致才会报错。
![mysql创建外键时报 SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint错误的解决方法](https://cdn.learnku.com/uploads/images/201910/21/23301/4dpEEzbA6C.png!large)
这是重新设置的表结构,执行后成功
本作品采用《CC 协议》,转载必须注明作者和本文链接
关于 LearnKu
推荐文章: