简体   繁体   中英

Why does MySQL give me a foreign key constraint error on a table that I don't have?

I'm running a series of upgrade scripts. As a note, in a an earlier script, there were many of these:

ALTER TABLE files DROP FOREIGN KEY files_ibfk_1, DROP FOREIGN KEY files_ibfk_2;

However, those keys did not exist….

Now I'm getting an error…:

Cannot add or update a child row: a foreign key constraint fails ( pacsdbcmi . #sql-536_77 , CONSTRAINT #sql-536_77_ibfk_1 FOREIGN KEY ( series_fk ) REFERENCES series ( pk ))

What is #sql-536_77 ? Can someone explain what this means? I do not have a table named #sql-536-77, nor a key in series called #sql-536_77_ibfk_1

Thanks

This is because there are another tables which is foreign to the parent table. CASCADE relation should be there.So before you ALTER the parent table foreign key please remove existing CASCADES

                                     OR

USE

SET foreign_key_checks = 0;

AND

ALTER TABLE files DROP FOREIGN KEY files_ibfk_1, DROP FOREIGN KEY files_ibfk_2;

THEN

SET foreign_key_checks = 1;

By setting the foreign key check to 0, you can able to ALTER table. Once It was done with operations on the table, you can reset the key check to 1 again and everything is back in place now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM