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

后端 未结 2 1314
囚心锁ツ
囚心锁ツ 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:15

    You've already got the tables linked, which is where the error is coming from. You need to make sure that you have a record in users_facebook BEFORE you try to insert a record into bugs with the same oauth_uid2, e.g.

    users_facebook has records with oauth_uid2 `10`, `20`, `30`
    

    you try to insert a record into bugs with

    INSERT INTO bugs (oauth_uid2) VALUES (10) // works, there's a matching record in users_facebook
    INSERT INTO bugs (oauth_uid2) VALUES (15) // fails, there's no user with that id.
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题