{"id":4214,"date":"2020-03-26T10:33:08","date_gmt":"2020-03-26T10:33:08","guid":{"rendered":"https:\/\/litextension.com\/faq\/docs\/shopping-cart-questions\/magento-1-2\/sqlstate23000-integrity-constraint-violation-1062-duplicate-entry\/"},"modified":"2026-05-18T07:32:43","modified_gmt":"2026-05-18T07:32:43","slug":"sqlstate23000-integrity-constraint-violation-1062-duplicate-entry","status":"publish","type":"docs","link":"https:\/\/litextension.com\/faq\/docs\/shopping-cart-questions\/magento-1-2\/sqlstate23000-integrity-constraint-violation-1062-duplicate-entry\/","title":{"rendered":"How to fix the \u201cSQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry\u201d error in Magento?"},"content":{"rendered":"\n<p>The <code>SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry<\/code> error is one of the more common database-related issues in Magento. In most cases, the error appears when Magento tries to insert duplicate data into a database table that requires unique values.<\/p>\n\n\n\n<p>This issue often happens during data migration, product imports, module installation, <code>setup:upgrade<\/code>, or URL rewrite generation. Since the exact fix depends on the affected table and the context of the error, there is no single solution that works for every case. However, the troubleshooting process is usually similar.<\/p>\n\n\n\n<p>Below, we will walk you through the typical approach Magento users take to identify and fix the duplicate entry issue.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Get the full error<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Look at the\u00a0<strong>full exception message<\/strong>\u00a0(from terminal,\u00a0<code>var\/log<\/code>, or\u00a0<code>var\/report<\/code>).<\/li>\n\n\n\n<li>You need the part that looks like: <code>Duplicate entry 'XYZ' for key 'some_table.SOME_INDEX'<\/code>\u00a0or\u00a0<code>'PRIMARY'<\/code>.<\/li>\n\n\n\n<li>Note the\u00a0<strong>table name<\/strong>,\u00a0<strong>duplicate value<\/strong>, and\u00a0<strong>index name<\/strong>\u00a0\u2013 that tells you where to fix data.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Find and remove the duplicate rows<\/strong><\/h3>\n\n\n\n<p>Once you identify the affected table, the next step is to check which rows contain the duplicate value. Before making any changes, create a database backup first, or at least back up the affected table.<\/p>\n\n\n\n<p>For example, if the error involves the <code>catalog_url_rewrite_product_category<\/code> table, you can inspect the duplicate rows with:<br><br><code>SELECT *<br>FROM catalog_url_rewrite_product_category<br>WHERE url_rewrite_id = '27698';<\/code><\/p>\n\n\n\n<p>After reviewing the results, decide which rows are safe to remove or update. In many Magento cases, duplicate URL rewrite rows are the main cause. One commonly used cleanup query looks like this:<\/p>\n\n\n\n<p><code>DELETE t1.*<br>FROM catalog_url_rewrite_product_category t1<br>INNER JOIN (<br>SELECT url_rewrite_id<br>FROM catalog_url_rewrite_product_category<br>GROUP BY url_rewrite_id<br>HAVING COUNT(*) > 1<br>) t2 ON t1.url_rewrite_id = t2.url_rewrite_id;<\/code><\/p>\n\n\n\n<p>This removes extra rows so only one row per\u00a0<code>url_rewrite_id<\/code>\u00a0remains. The same logic applies to other Magento tables as well. You first identify rows using the duplicated key, then remove or correct the invalid entries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Re-run the Magento action<\/strong><\/h3>\n\n\n\n<p>After cleaning the duplicate data, rerun the action that originally failed. Depending on your situation, this may involve:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Running <code>setup:upgrade<\/code> <\/li>\n\n\n\n<li>Re-importing products or orders <\/li>\n\n\n\n<li>Reindexing Magento <\/li>\n\n\n\n<li>Re-enabling a module<\/li>\n<\/ul>\n\n\n\n<p>If the duplicate rows were fixed correctly, Magento should complete the process normally.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4<\/strong>. <strong>Replace the <code>toolbar.phtml<\/code> file<\/strong> <strong>(optional)<\/strong><\/h3>\n\n\n\n<p>If the error is related to your Magento theme files, you can also try removing:<br><code> <i>\/app\/design\/frontend\/default\/your theme\/template\/catalog\/product\/list\/toolbar.phtml<\/i><\/code><br> and replace it with:<br><code> <i>app\/design\/frontend\/base\/default\/template\/catalog\/product\/list\/toolbar.phtml<\/i><\/code><\/p>\n\n\n\n<p>In case you have any other questions, please reach out to us via:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/litextension.zendesk.com\/hc\/en-us\/signin?return_to=https%3A%2F%2Flitextension.zendesk.com%2Fhc%2Fen-us%2Frequests\" target=\"_blank\" rel=\"noopener\">Ticket system<\/a> (highly recommended)<\/li>\n\n\n\n<li>Email us at contact@litextension.com<\/li>\n<\/ul>\n","protected":false},"author":1,"featured_media":0,"parent":20,"menu_order":24,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"doc_tag":[],"class_list":["post-4214","docs","type-docs","status-publish","hentry"],"comment_count":0,"_links":{"self":[{"href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/docs\/4214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/comments?post=4214"}],"version-history":[{"count":2,"href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/docs\/4214\/revisions"}],"predecessor-version":[{"id":7561,"href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/docs\/4214\/revisions\/7561"}],"up":[{"embeddable":true,"href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/docs\/20"}],"prev":[{"title":"Why the number of products shown on Magento changes after migration?","link":"https:\/\/litextension.com\/faq\/docs\/shopping-cart-questions\/magento-1-2\/why-number-of-products-after-migration-change-after-magento-migration\/","href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/docs\/4212"}],"wp:attachment":[{"href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/media?parent=4214"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/litextension.com\/faq\/wp-json\/wp\/v2\/doc_tag?post=4214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}