1

We are getting errors like

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'themen/redacted/redacted.html-1' for key 'URL_REWRITE_REQUEST_PATH_STORE_ID', query was: INSERT  INTO `url_rewrite` (`redirect_type`,`is_autogenerated`,`metadata`,`description`,`entity_type`,`entity_id`,`request_path`,`target_path`,`store_id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?), (

with 8000 params in the SQL when updating a single category.

Are there still known issues in Magento 2.4.3 concering URL rewrites? How can this be debugged and fixed?

For debugging I replaced \Magento\Framework\DB\Statement\Pdo\Mysql::_execute

public function _execute(array $params = null)
{
    $specialExecute = false;
    if ($params) {
        foreach ($params as $param) {
            if ($param instanceof Parameter) {
                $specialExecute = true;
                break;
            }
        }
    }

    if ($specialExecute) {
        return $this->_executeWithBinding($params);
    } else {
        try {
            return $this->tryExecute(function () use ($params) {
                return $params !== null ? $this->_stmt->execute($params) : $this->_stmt->execute();
            });
        } catch (\Exception $e) {

            if (strpos($e->getMessage(), 'URL_REWRITE_REQUEST_PATH_STORE_ID') !== false) {
                $keys = [];
                foreach(range(0,(count($params) / 9) -1) as $index) {
                    $keys[] = $params[$index * 9 + 6] . '-' . $params[$index * 9 + 8];
                }

            }


            throw new \Exception('Key list:' . PHP_EOL . implode(PHP_EOL, $keys) . PHP_EOL,0,$e);
        }
    }
}

This shows duplicates with the same words but once with a capital letter, once lower case.

EDIT:

First problem solved, new problem arises

We deleted URL paths and regenerated all URLs using https://github.com/elgentos/regenerate-catalog-urls, this fixes the first problem. but now we get

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '51541850' for key 'PRIMARY', query was:
INSERT  INTO `catalog_url_rewrite_product_category` 
(`url_rewrite_id`,`category_id`,`product_id`) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?)

Debugging

The above error is caused in \Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product::saveMultiple which is called by \Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage::afterReplace and is an interceptor of \Magento\UrlRewrite\Model\UrlPersistInterface::replace

The question I ask myself is: It's intercepting a replace call, but actually it is inserting the keys to the catalog_url_rewrite_product_category table - not using a replace into - is this a logic bug in the current Magento version?

2 Answers 2

1

I had the same issue 'Unique constraint...' while changing URL keys for categories.

Used Oleg's extension: https://github.com/olegkoval/magento2-regenerate_url_rewrites

php bin/magento ok:urlrewrites:regenerate --no-regen-url-key

I ran it first with --entity-type=category & then without (default).

Resolved !

Note: If you don't use --no-regen-url-key, all your URL Key fields in Magento Admin will be removed !

0

This query helped here, after the upgrade:

https://github.com/magento/magento2/issues/33770#issuecomment-991065898

START TRANSACTION;
CREATE TABLE catalog_url_rewrite_product_category_temp
SELECT DISTINCT *
FROM catalog_url_rewrite_product_category;
ALTER TABLE catalog_url_rewrite_product_category RENAME catalog_url_rewrite_product_category_backup;
ALTER TABLE catalog_url_rewrite_product_category_temp RENAME catalog_url_rewrite_product_category;
DROP TABLE catalog_url_rewrite_product_category_backup;
COMMIT;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.