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

前端 未结 21 2725
生来不讨喜
生来不讨喜 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条回答
  •  Happy的楠姐
    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;
    

提交回复
热议问题