How to Fix Foreign Key Constraint Fails in Magento

I am in the stage of migrating my local database of magento to public when I have encountered this error.

How did it happen:
1.) I use phpmyadmin3.2 to export the sql statements.
2.) In my host mysql's phpmyadmin, I have executed the dump statements.

And I got the following error:
SQL query:


--
-- Constraints for table `eav_entity_attribute`
--
ALTER TABLE `eav_entity_attribute` ADD CONSTRAINT `FK_EAV_ENTITY_ATTRIBUTE_ATTRIBUTE` FOREIGN KEY ( `attribute_id` ) REFERENCES `eav_attribute` ( `attribute_id` ) ON DELETE CASCADE ON UPDATE CASCADE ,
ADD CONSTRAINT `FK_EAV_ENTITY_ATTRIBUTE_GROUP` FOREIGN KEY ( `attribute_group_id` ) REFERENCES `eav_attribute_group` ( `attribute_group_id` ) ON DELETE CASCADE ON UPDATE CASCADE

MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`db294125795/#sql-1357_3238a8`, CONSTRAINT `FK_EAV_ENTITY_ATTRIBUTE_ATTRIBUTE` FOREIGN KEY (`attribute_id`) REFERENCES `eav_attribute` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE)


So what did I do:
1.) I simply re-dumped my local database sql statements using phpmyadmin this time checking "Disable foreign key contraints checkbox" and it executed without error.

See attached image for reference.

18 Comments

  1. Thanks, I was having the same issue migrating my magento database. now, all the queries were imported successfully. I appreciate the time you took to publish this post. cheers.

    ReplyDelete
  2. I was wondering if there is a solution for this.
    Very helpful.
    Thanks

    ReplyDelete
  3. Thanks! Worked like a charm. :)

    ReplyDelete
  4. Amaaazing! Thank you so much

    ReplyDelete
  5. And again: THANKS!!!

    ReplyDelete
  6. Thank you so much for posting this. I have a launch tomorrow and I couldn't upload the site without this.

    ReplyDelete
  7. Awesome. This saved me a lot of head-scratching!

    ReplyDelete
  8. awesome... you've just saved me lot's of time. Used your example to get syntax SET FOREIGN_KEY_CHECKS=0; which fixed the import error I was getting when I pasted the line into a data dump from Template Monster that wouldn't upload using phpmyadmin or using command line.

    ReplyDelete
  9. Hey Friend,

    it's really great help, great job ;)

    ReplyDelete
  10. As somebody on the thread says, just adding SET FOREIGN_KEY_CHECKS=0 at the top of the commands fixes this issue.

    ReplyDelete
  11. thanks that worked!

    ReplyDelete
  12. You're awesome! Thank you for posting this fix!

    ReplyDelete

Post a Comment

Post a Comment

Previous Post Next Post
NERV Open Source

Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.

Explore NERV on GitHub →