表的级联没有添加ON DELETE CASCADE ON UPDATE CASCADE ;在删除主表时会报Cannot delete or update a parent row: a foreign key constraint fails

原来的结构是CONSTRAINT `FK_cgqbhur8b7r95m7o126opuyrf` FOREIGN KEY (`park_id`) REFERENCES `park` (`id`) ;

修改后为CONSTRAINT `FK_cgqbhur8b7r95m7o126opuyrf` FOREIGN KEY (`park_id`) REFERENCES `park` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ;

先删除外键,再增加外键

级联删除报Cannot delete or update a parent row: a foreign key constraint fails解决方法

alter table park_datasourcetype drop foreign key FK_cgqbhur8b7r95m7o126opuyrf

ALTER TABLE park_datasourcetype add  CONSTRAINT `FK_cgqbhur8b7r95m7o126opuyrf` FOREIGN KEY (`park_id`) REFERENCES `park` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ;


alter table park_datasourcetype drop foreign key FK_8ttovfgp0pp8v6ghj0pjr0w36
ALTER TABLE park_datasourcetype add  CONSTRAINT `FK_8ttovfgp0pp8v6ghj0pjr0w36` FOREIGN KEY (`datasourcetype_id`) REFERENCES `datasourcetype` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ;


alter table buildingtype drop foreign key FKC58027EE208F269E

ALTER TABLE buildingtype add  CONSTRAINT `FKC58027EE208F269E` FOREIGN KEY (`park_id`) REFERENCES `park` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ;


相关文章

http://blog.csdn.net/hitman9099/article/details/5773821

http://www.cnblogs.com/chang1203/p/5881905.html

http://blog.csdn.net/lovesomnus/article/details/22934009

相关文章: