Articles on: Magento 2 User Guide

How to fix Integrity constraint violation when upgrading Magento from 2.1.x to 2.2.x

This issue isn’t related to WeltPixel extensions; it’s a Magento core upgrade problem that can appear when moving from Magento 2.1.x → 2.2.x.



Error you may see during setup:upgrade


Module 'Magento_SalesRule':

Upgrading data.. SQLSTATE[23000]: Integrity constraint violation:

1062 Duplicate entry '0-0-0-0' for key 'PRIMARY', query was:

INSERT INTO salesrule_product_attribute () VALUES ()


This happens when the upgrade step attempts to insert empty/duplicate rows into salesrule_product_attribute, which causes a primary-key collision.



Solution 1 — Guard against empty inserts (minimal, reversible vendor edit)


File to edit:

vendor/magento/module-sales-rule/Model/ResourceModel/Rule.php (around line 352)


Original line:


$connection->insertMultiple($this->getTable('salesrule_product_attribute'), $data);


Wrap it with a condition to skip empty inserts:


if (!empty($data)) {

$connection->insertMultiple($this->getTable('salesrule_product_attribute'), $data);

}


Then run:


php bin/magento setup:upgrade


Tip: This is a temporary change to allow the upgrade to finish. After completion, you can revert the vendor edit (or keep it until you complete all upgrade steps and retest).



Solution 2 — Truncate conflicted tables (destructive; back up first)


Important: The following deletes data related to sales rules and coupons. Back up your database first and export these tables if you need to re-create rules later.


Run the following SQL in your DB tool (e.g., phpMyAdmin), then upgrade:


TRUNCATE TABLE salesrule;

TRUNCATE TABLE salesrule_coupon;

TRUNCATE TABLE salesrule_coupon_usage;

TRUNCATE TABLE salesrule_customer;

TRUNCATE TABLE salesrule_product_attribute;


Finish with:


php bin/magento setup:upgrade



Notes & recommendations


  • Always test the upgrade on a staging copy before running in production.
  • Back up both code and database.
  • If you used Solution 1, consider reverting the vendor change after the upgrade and re-running a quick setup:upgrade, then cache:flush, to keep your vendor code pristine.
  • Recreate or reimport any needed cart price rules if you used Solution 2.

Updated on: 17/09/2025

Was this article helpful?

Share your feedback

Cancel

Thank you!