MySQL Error 1215: Cannot add foreign key constraint

前端 未结 30 2839
予麋鹿
予麋鹿 2020-11-22 01:37

I am trying to forward engineer my new schema onto my db server, but I can\'t figure out why I am getting this error. I\'ve tried to search for the answer here, but everyth

30条回答
  •  鱼传尺愫
    2020-11-22 02:02

    For me, the 1215 error occurred when I was importing a dumpfile created by mysqldump, which creates the tables alphabetically, which in my case, caused foreign keys to reference tables created later in the file. (Props to this page for pointing it out: https://www.percona.com/blog/2017/04/06/dealing-mysql-error-code-1215-cannot-add-foreign-key-constraint/)

    Since mysqldump orders tables alphabetically and I did not want to change the names of tables, I followed the instructions in the answer by JeremyWeir on this page, which states to put set FOREIGN_KEY_CHECKS = 0; at the top of the dump file and put SET FOREIGN_KEY_CHECKS = 1; at the bottom of the dump file.

    That solution worked for me.

提交回复
热议问题