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

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

    While inserting the foreign key attribute values, first verify the attributes type, as well as primary key attribute value in the parent relation, if the values in parent relation matches, then you can easily insert/update child attribute values.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-11-22 07:42

    Its 100% working...

    ERROR 1452: Cannot add or update a child row: a foreign key constraint fails.... if this type of error occur : then First of all goto this table and check in setting > if Engine is: InnoDB then change its to MyISAM

    (and delete references foreign constraint)

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

    You must delete data in the child table which does not have any corresponding foreign key value to the parent table primary key .Or delete all data from the child table then insert new data having the same foreign key value as the primary key in the parent table . That should work . Here also a youtube video

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

    you should insert at least one raw in each tables (the ones you want the foreign keys pointing at) then you can insert or update the values of the foreign keys

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

    First allow NULL on the parent table and set the default values to NULL. Next create the foreign key relationship. Afterwards, you can update the values to match accordingly

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