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

后端 未结 21 1735
野性不改
野性不改 2020-11-22 07:01

I have created tables in MySQL Workbench as shown below :

ORDRE table:

CREATE TABLE Ordre (
  OrdreID   INT NOT NULL,
  OrdreDato DA         


        
相关标签:
21条回答
  • 2020-11-22 07:31

    This can be fixed by inserting the respective records in the Parent table first and then we can insert records in the Child table's respective column. Also check the data type and size of the column. It should be same as the parent table column,even the engine and collation should also be the same. TRY THIS! This is how I solved mine. Correct me if am wrong.

    0 讨论(0)
  • 2020-11-22 07:31

    check the no. of record in parent table that matches with child table and also primary key must match with foreign key reference. This works for me.

    0 讨论(0)
  • 2020-11-22 07:33

    in the foreign key table has a value that is not owned in the primary key table that will be related, so you must delete all data first / adjust the value of your foreign key table according to the value that is in your primary key

    0 讨论(0)
  • 2020-11-22 07:35

    The Problem is with FOREIGN KEY Constraint. By Default (SET FOREIGN_KEY_CHECKS = 1). FOREIGN_KEY_CHECKS option specifies whether or not to check foreign key constraints for InnoDB tables. MySQL - SET FOREIGN_KEY_CHECKS

    We can set foreign key check as disable before running Query. Disable Foreign key.

    Execute one of these lines before running your query, then you can run your query successfully. :)

    1) For Session (recommended)

    SET FOREIGN_KEY_CHECKS=0;
    

    2) Globally

    SET GLOBAL FOREIGN_KEY_CHECKS=0;
    
    0 讨论(0)
  • 2020-11-22 07:36

    I had the same problem. I was creating relationships on existing tables but had different column values, which were supposed/assumed to be related. For example, I had a table USERS that had a column USERID with rows 1,2,3,4,5. Then I had another child table ORDERS with a column USERID with rows 1,2,3,4,5,6,7. Then I run MySQl command ALTER TABLE ORDERS ADD CONSTRAINT ORDER_TO_USER_CONS FOREIGN KEY (ORDERUSERID) REFERENCES USERS(USERID) ON DELETE SET NULL ON UPDATE CASCADE;

    It was rejected with the message:

    Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (DBNAME1.#sql-4c73_c0, CONSTRAINT ORDER_TO_USER_CONS FOREIGN KEY (ORDERUSERID) REFERENCES USERS (USERID) ON DELETE SET NULL ON UPDATE CASCADE)

    I exported data from the ORDERS table, then deleted all data from it, re-run the command again, it worked this time, then re-inserted the data with the corresponding USERIDs from the USERS table.

    0 讨论(0)
  • 2020-11-22 07:38

    When you're using foreign key, your order of columns should be same for insertion.

    For example, if you're adding (userid, password) in table1 from table2 then from table2 order should be same (userid, password) and not like (password,userid) where userid is foreign key in table2 of table1.

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