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

后端 未结 2 1316
囚心锁ツ
囚心锁ツ 2020-12-11 11:42

When I wan\'t to connect two tables with eachother then I get the message : #1452 - Cannot add or update a child row: a foreign key constraint fails....I want to connect oau

2条回答
  •  离开以前
    2020-12-11 12:25

    If you backup your Magento database using other tools, like phpMyAdmin or Navicat, these special statements will be missing. When you attempt to run the .sql file, you will get errors like these:

    Cannot add or update a child row: a foreign key constraint fails

    This error occurs because the data you are importing is provided table by table, row by row, without regard to the logical structure and integrity of the database.

    To restore a .sql file backup without constraint checking, simply add the following statements at the beginning of your .sql file:

    SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT;
    SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS;
    SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;
    SET NAMES utf8;
    SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
    SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
    SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
    SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0;
    

    At the end of the file, add the statements required to turn on constraint checking again:

    SET SQL_MODE=@OLD_SQL_MODE;
    SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
    SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
    SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT;
    SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS;
    SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION;
    SET SQL_NOTES=@OLD_SQL_NOTES;
    

    you will be able to do this.

提交回复
热议问题