I am seeing this error when trying to perform a search/replace on the url_rewrites table:

UPDATE url_rewrite SET request_path = REPLACE(request_path, '.html/', '/');

SQL Error (1062): Duplicate entry 'myurl' for key 'URL_REWRITE_REQUEST_PATH_STORE_ID'

(I need to do this because my URLs are messed up and being formatted like this: category1/category2.html/category3/product-url)

I understand that there are unique constraints on this table but when I search for the offending rewrite that is given in the error message it is not found.

SELECT * FROM url_rewrite WHERE request_path LIKE '%myurl etc etc%';

So there is my confusion! How can it be a duplicate if there is not one there already?!

(Magento 2.2.5)

1 Answer 1

SHOW CREATE TABLE url_rewrite;


| url_rewrite | CREATE TABLE `url_rewrite` (
  `url_rewrite_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Rewrite ID',
  `entity_type` varchar(32) NOT NULL COMMENT 'Entity type code',
  `entity_id` int(10) unsigned NOT NULL COMMENT 'Entity ID',
  `request_path` varchar(255) DEFAULT NULL COMMENT 'Request Path',
  `target_path` varchar(255) DEFAULT NULL COMMENT 'Target Path',
  `redirect_type` smallint(5) unsigned NOT NULL DEFAULT 0 COMMENT 'Redirect Type',
  `store_id` smallint(5) unsigned NOT NULL COMMENT 'Store ID',
  `description` varchar(255) DEFAULT NULL COMMENT 'Description',
  `is_autogenerated` smallint(5) unsigned NOT NULL DEFAULT 0 COMMENT 'Is rewrite generated automatically flag',
  `metadata` varchar(255) DEFAULT NULL COMMENT 'Meta data for url rewrite',
  PRIMARY KEY (`url_rewrite_id`),
  UNIQUE KEY `URL_REWRITE_REQUEST_PATH_STORE_ID` (`request_path`,`store_id`),
  KEY `URL_REWRITE_TARGET_PATH` (`target_path`),
  KEY `URL_REWRITE_STORE_ID_ENTITY_ID` (`store_id`,`entity_id`),
  KEY `URL_REWRITE_ENTITY_ID` (`entity_id`)

Note the UNIQUE KEY definition in the above output:

UNIQUE KEY `URL_REWRITE_REQUEST_PATH_STORE_ID` (`request_path`,`store_id`)

So, you should also check store_id value in your url_rewrite table.

For example, if you have this in url_rewrite table:

request_path = 'my-product'
store_id = 11

then, you might get error like:

Duplicate entry 'my-product-11' for key 'URL_REWRITE_REQUEST_PATH_STORE_ID'

This might create confusion if we don't look at the table definition for the unique key.

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.