简体   繁体   中英

Spring boot schema.sql not working with mysqldump file

Spring boot automatically picked up and ran my schema.sql file everytime my integration test is started.

Issue is:
It gives me an error

Cannot delete or update a parent row: a foreign key constraint fails.

I am able to import this file with mysql command without problem, so I suspect that Spring boot is not using mysql command to run the script.
schema.sql is created by mysqldump . Nothing is modified.
It works after I did the following "workaround":
1. Get rid of MySQL specific keywords/commands like /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
2. re-arrange the create table blocks so that the tables are created in the right order to avoid the foreign key constraint errors.

Question : How to make Spring boot to import my schema.sql file like mysql does? It would be nice if I can just mysqldump the file and use it for testing instead of using some "hacky workarounds".

Thank you!

Add in the beginning of your .sql file

SET FOREIGN_KEY_CHECKS=0;

and in the end

SET FOREIGN_KEY_CHECKS=1;

The command disables foreign keys checks and restores it back.

Dump just inserts all the table's content rows but related table is not inserted yet.

MYSQL restoring from dump has the lines automatically.

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