Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails

前端 未结 21 2284
生来不讨喜
生来不讨喜 2020-11-22 01:32

I\'m having a bit of a strange problem. I\'m trying to add a foreign key to one table that references another, but it is failing for some reason. With my limited knowledge o

21条回答
  •  温柔的废话
    2020-11-22 02:13

    you can try this exapmple

     START TRANSACTION;
     SET foreign_key_checks = 0;
     ALTER TABLE `job_definers` ADD CONSTRAINT `job_cities_foreign` FOREIGN KEY 
     (`job_cities`) REFERENCES `drop_down_lists`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
     SET foreign_key_checks = 1;
     COMMIT;
    

    Note : if you are using phpmyadmin just uncheck Enable foreign key checks

    as example

    hope this soloution fix your problem :)

提交回复
热议问题