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

前端 未结 21 2086
生来不讨喜
生来不讨喜 2020-11-22 01:32

I\'m having a bit of a strange problem. I\'m trying to add a foreign key to one table that references another, but it is failing for some reason. With my limited knowledge o

相关标签:
21条回答
  • 2020-11-22 02:10

    I had this exact same problem about three different times. In each instance it was because one (or more) of my records did not conform to the new foreign key. You may want to update your existing records to follow the syntax constraints of the foreign key before trying to add the key itself. The following example should generally isolate the problem records:

    SELECT * FROM (tablename)
        WHERE (candidate key) <> (proposed foreign key value) 
            AND (candidate key) <> (next proposed foreign key value)
    

    repeat AND (candidate key) <> (next proposed foreign key value) within your query for each value in the foreign key.

    If you have a ton of records this can be difficult, but if your table is reasonably small it shouldn't take too long. I'm not super amazing in SQL syntax, but this has always isolated the issue for me.

    0 讨论(0)
  • 2020-11-22 02:11

    I had the same issue with my MySQL database but finally, I got a solution which worked for me.
    Since in my table everything was fine from the mysql point of view(both tables should use InnoDB engine and the datatype of each column should be of the same type which takes part in foreign key constraint).
    The only thing that I did was to disable the foreign key check and later on enabled it after performing the foreign key operation.
    Steps that I took:

    SET foreign_key_checks = 0;
    
    alter table tblUsedDestination add constraint f_operatorId foreign key(iOperatorId) references tblOperators (iOperatorId); Query
    OK, 8 rows affected (0.23 sec) Records: 8  Duplicates: 0  Warnings: 0
    
    SET foreign_key_checks = 1;
    
    0 讨论(0)
  • 2020-11-22 02:13

    Empty both your tables' data and run the command. It will work.

    0 讨论(0)
  • 2020-11-22 02:13

    you can try this exapmple

     START TRANSACTION;
     SET foreign_key_checks = 0;
     ALTER TABLE `job_definers` ADD CONSTRAINT `job_cities_foreign` FOREIGN KEY 
     (`job_cities`) REFERENCES `drop_down_lists`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
     SET foreign_key_checks = 1;
     COMMIT;
    

    Note : if you are using phpmyadmin just uncheck Enable foreign key checks

    as example

    hope this soloution fix your problem :)

    0 讨论(0)
  • 2020-11-22 02:14

    try this

    SET foreign_key_checks = 0;
    
    ALTER TABLE sourcecodes_tags ADD FOREIGN KEY (sourcecode_id) REFERENCES sourcecodes (id) ON DELETE CASCADE ON UPDATE CASCADE
    
    SET foreign_key_checks = 1;
    
    0 讨论(0)
  • 2020-11-22 02:14

    I was getting this error when using Laravel and eloquent, trying to make a foreign key link would cause a 1452. The problem was lack of data in the linked table.

    Please see here for an example: http://mstd.eu/index.php/2016/12/02/laravel-eloquent-integrity-constraint-violation-1452-foreign-key-constraint/

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