Being a Magento 2 store admin, you might have faced an error stating, “Unique Constraint violation found.”
In the backend, it looks something like as shown here:

You may have got an integrity constraint violation error while creating an order from the backend in Magento 2 store.
There can be multiple times this issue might occur, I have seen some of the Magento store owners facing such issues while adding any products, categories, Megamenu items, or creating a new order.
Here is a solution to fix the unique constraint violation in Magento 2 admin panel for two main scenarios.
- Scenario 1: Fix the unique constraint violation in Magento 2 (Integrity Constraint Violation)
- Scenario 2: Fix the unique constraint violation in Magento 2 while adding any products, categories, or Megamenu items.
Scenario 1: Fix the unique constraint violation in Magento 2 (Integrity Constraint Violation)
An integrity constraint violation error occurs when you try to violate the defined constraints in the Magento database schema. These constraints are there to avoid duplicate or conflicting entries in your database. If you are constantly facing this error, here are the steps to overcome it.
Steps to Fix Unique Constraint Violation Found in Magento 2
Check the table “sequence_order_1” where 1 refers to the store id.

In the above table, the sequence_value column should be auto_increment to fix the integrity constraint violation in Magento 2.
Scenario 2: Fix the unique constraint violation in Magento 2 while adding any products, categories, or Megamenu items.
This issue occurs when you insert data that violates the unique constraints defined in the Magento database.
The solution here is to first delete the existing url_rewrites, the function removeMultiple already exists.
And then, overwrite MagentoCatalogUrlRewriteModelCategoryPluginStorage and add this line before the saveMultiple.
$this->productResource->removeMultiple(array_column($toSave, ‘url_rewrite_id’));
Which will then look like this:
if (count($toSave) > 0) { $this->productResource->removeMultiple(array_column($toSave, 'url_rewrite_id')); $this->productResource->saveMultiple($toSave); }
& that’s it. This is how you can fix the unique constraint violation in Magento 2 based on these two scenarios. Both scenarios focus on avoiding any duplicate records or similar records that can cause an issue in your Magento 2 store.
If the solution was helpful to you, do share it with the Magento Community via social media. Thank you!
✨Related read: