How to Fix Unique Constraint Violation Found in Magento 2
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 Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage and add this line before the saveMultiple.
$this->productResource->removeMultiple(array_column($toSave, ‘url_rewrite_id’));
Which will then look like this:
1 2 3 4 |
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 you have any doubts about the error and its solution, you can mention it in the comments section below. I’d be happy to help you and guide you through this process.
If the solution was helpful to you, do share it with the Magento Community via social media. Thank you!
✨Related read:
1. How to Create Magento 2 Order Status & Order State
2. How to Get Order Information By Order Id in Magento 2
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
8 Comments
Hi,
Thank you for this article. I have checked and I have AUTO_INCREMENT on all of these mget_sequence_order_0 through mget_sequence_7.
I have documented my case here if you would please take a look?
https://magento.stackexchange.com/questions/349820/unique-constraint-violation-found-when-updating-existing-orders
Thank you,
Allysin
Hello Allysin,
Search the error in the default Magento as below:
https://drops.meetanshi.com/LGLyev
and add the logs in every file to identify the exact cause of the error and
return the error using $e->getMessage().
Using that, the actual error will display and you can find the way of the exact solution
Thank You
Hi Sanjay,
I’m afraid this is beyond of what I’m able to do. How much would you charge me to fix this?
Have a great day,
Allysin
Hello Allysin,
I’ve replied to you on [email protected] email.
Please check.
Thank You
Hi,
I tried what you suggested but continue to get the same error still.
Unique constraint violation found
Running Magento 2.3.5 version and the table sequence_order_1 did have the problem so on making it auto increment and primary key thought it would solve the problem. Also applied Analyze table operation that fixed the cardinality value to the correct count but still the same error.
So admin login not working and giving same error. Any help would be appreciated.
Thanks.
Hello Vishal,
Please verify there’s not the same increment ID on 2 orders.
Thank You
Thanks You So much for this quick solution.
Hello Kirandeep,
I’m glad to help you out.
Thank You