Ignore MySQL foreign key constraints in PHP

前端 未结 4 1794
生来不讨喜
生来不讨喜 2021-02-19 10:17

Is there a way to override mysql foreign key constraints in a php script?

I have a query passed to mysql from php, but it fails a foreign key constraint, is there any wa

4条回答
  •  醉话见心
    2021-02-19 11:01

    Run the query: set FOREIGN_KEY_CHECKS=0;

    mysql> insert into bar values(1);
    ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`bar`, CONSTRAINT `bar_ibfk_1` FOREIGN KEY (`foo_id`) REFERENCES `foo` (`foo_id`) ON UPDATE CASCADE)
    mysql> set FOREIGN_KEY_CHECKS=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> insert into bar values(1);
    Query OK, 1 row affected (0.00 sec)
    

提交回复
热议问题