How to Fix Magento Upgrade Error: SQLSTATE[23000]: Integrity constraint violation

This error is very common when upgrading Magento. We recently came across it when upgrading a client from 1.5.0.1 to 1.6.2.0 but it can happy for any version. The error typically looks like the following:

SQLSTATE[23000]: Integrity constraint violation:

To fix it, first try clearing your cache and try loading your site so the upgrade process can restart. If that doesn’t work, you need to update the initStatements in app/etc/config.xml. Change:

[sourcecode language=”xml”]
<initStatements>SET NAMES utf8</initStatements>
[/sourcecode]

to

[sourcecode language=”xml”]<initStatements>SET NAMES utf8; SET FOREIGN_KEY_CHECKS=0; SET UNIQUE_CHECKS=0;</initStatements>[/sourcecode]

That should do the trick. Make sure you change it back once the upgrade is done. If you’re still having problems, check out our tips for upgrading Magento. If that doesn’t help you, leave a comment and we’ll try to point you in the right direction