1. Home
  2. Specific Platform Questio...
  3. Magento/Adobe Commerce
  4. How to fix the “SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry” error in Magento?

How to fix the “SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry” error in Magento?

The SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 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.

This issue often happens during data migration, product imports, module installation, setup:upgrade, 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.

Below, we will walk you through the typical approach Magento users take to identify and fix the duplicate entry issue.

1. Get the full error

  • Look at the full exception message (from terminal, var/log, or var/report).
  • You need the part that looks like: Duplicate entry 'XYZ' for key 'some_table.SOME_INDEX' or 'PRIMARY'.
  • Note the table nameduplicate value, and index name – that tells you where to fix data.

2. Find and remove the duplicate rows

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.

For example, if the error involves the catalog_url_rewrite_product_category table, you can inspect the duplicate rows with:

SELECT *
FROM catalog_url_rewrite_product_category
WHERE url_rewrite_id = '27698';

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:

DELETE t1.*
FROM catalog_url_rewrite_product_category t1
INNER JOIN (
SELECT url_rewrite_id
FROM catalog_url_rewrite_product_category
GROUP BY url_rewrite_id
HAVING COUNT(*) > 1
) t2 ON t1.url_rewrite_id = t2.url_rewrite_id;

This removes extra rows so only one row per url_rewrite_id remains. 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.

3. Re-run the Magento action

After cleaning the duplicate data, rerun the action that originally failed. Depending on your situation, this may involve:

  • Running setup:upgrade
  • Re-importing products or orders
  • Reindexing Magento
  • Re-enabling a module

If the duplicate rows were fixed correctly, Magento should complete the process normally.

4. Replace the toolbar.phtml file (optional)

If the error is related to your Magento theme files, you can also try removing:
/app/design/frontend/default/your theme/template/catalog/product/list/toolbar.phtml
and replace it with:
app/design/frontend/base/default/template/catalog/product/list/toolbar.phtml

In case you have any other questions, please reach out to us via:

Was this article helpful to you? No Yes

How can we help?