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

后端 未结 21 2006
野性不改
野性不改 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:41

    In my case the tables were perfectly consistent.

    Anyway I was getting this error because I created (by accident) more than one FK constraint on the same field.

    I run the following query to show all the keys:

    SELECT *
    FROM information_schema.table_constraints
    WHERE constraint_schema = 'my_db_name'
    

    and I deleted the wrong ones with the following query:

    ALTER TABLE my_table
    DROP FOREIGN KEY wrong_fk_constraint; 
    

    You can check it also running this query:

    SHOW CREATE TABLE my_table;
    

提交回复
热议问题