How to bypass the error “Cannot add or update a child row: a foreign key constraint fails” when importing a MySQL dump file

If you encounter the error “Cannot add or update a child row: a foreign key constraint fails” when you import a MySQL dump file, you can bypass the foreign key checks by setting the following line at the top of your MySQL dump file:

SET foreign_key_checks = 0;

Note that the line should be put before any other SQL query in the dump. Save the file and try to import it once again. This time it should be imported silently.

If you are proficient in the SQL, you might try and suggest other alternative solutions.

A short update:

The above solution might be rough and not well working. If you experience troubles and errors after the import, you can try to replace the above line with:

SET SQL_MODE=”NO_AUTO_VALUE_ON_ZERO”;

The second solution worked better for me particularly.