{"id":1329,"date":"2020-10-29T07:29:46","date_gmt":"2020-10-29T07:29:46","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/10\/29\/unique-constraint-violation-found-in-magento-2\/"},"modified":"2025-05-22T11:35:17","modified_gmt":"2025-05-22T06:05:17","slug":"unique-constraint-violation-found-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/unique-constraint-violation-found-in-magento-2\/","title":{"rendered":"How to Fix Unique Constraint Violation Found in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Being a&nbsp;<a href=\"https:\/\/business.adobe.com\/in\/products\/magento\/magento-commerce.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2<\/a>&nbsp;store admin, you might have faced an error stating, \u201cUnique Constraint violation found.\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the backend, it looks something like as shown here:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/10\/Unique-Constraint-Violation-Found-in-Magento-2.png\" alt=\"Unique Constraint Violation Found in Magento 2\" class=\"wp-image-11168\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You may have got an integrity constraint violation error while creating an order from the backend in Magento 2 store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a solution to&nbsp;fix the unique constraint violation in Magento 2&nbsp;admin panel for two main scenarios.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Scenario 1: Fix the unique constraint violation in Magento 2 (Integrity Constraint Violation)<\/li>\n\n\n\n<li>Scenario 2: Fix the unique constraint violation in Magento 2 while adding any products, categories, or Megamenu items.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Scenario-1\">Scenario 1: Fix the unique constraint violation in Magento 2 (Integrity Constraint Violation)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Steps to Fix Unique Constraint Violation Found in Magento 2<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Check the table \u201c<strong>sequence_order_1<\/strong>\u201d where 1 refers to the store id.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/10\/sequence_order_1.png\" alt=\"sequence_order_1\" class=\"wp-image-11131\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the above table, the sequence_value column should be&nbsp;<em><strong>auto_increment<\/strong><\/em>&nbsp;to fix the integrity constraint violation in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Scenario-2\">Scenario 2: Fix the unique constraint violation in Magento 2 while adding any products, categories, or Megamenu items.<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This issue occurs when you insert data that violates the unique constraints defined in the Magento database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The solution here is to first delete the existing&nbsp; url_rewrites, the function removeMultiple already exists.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And then, overwrite MagentoCatalogUrlRewriteModelCategoryPluginStorage and add this line before the saveMultiple.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>$this-&gt;productResource-&gt;removeMultiple(array_column($toSave, \u2018url_rewrite_id\u2019));&nbsp;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Which will then look like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (count($toSave) > 0) {\n$this->productResource->removeMultiple(array_column($toSave, 'url_rewrite_id'));\n$this->productResource->saveMultiple($toSave);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&amp; that\u2019s 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the solution was helpful to you, do share it with the Magento Community via social media. Thank you!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2728Related read:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.<a href=\"https:\/\/meetanshi.com\/blog\/create-magento-2-order-status-order-state\/\" target=\"_blank\" rel=\"noreferrer noopener\">&nbsp;How to Create Magento 2 Order Status &amp; Order State<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2.&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/get-order-information-by-order-id-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Get Order Information By Order Id in Magento 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Being a&nbsp;Magento 2&nbsp;store admin, you might have faced an error stating, \u201cUnique Constraint violation found.\u201d In the backend, it looks something like as shown here:&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1329","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1329"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1329\/revisions"}],"predecessor-version":[{"id":14636,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1329\/revisions\/14636"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}