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?