Magento – An error occurred while saving the URL rewrite

catalogce-1.7.0.2MySQL

I have a store running 1.7.0.2 (updated from a clean install of 1.6). The store keeps running into the "An error occurred while saving the URL rewrite" error.

The store is a very simple installation with only premium matrix rate installed as an additional module.

Error:

On trying to refresh the Catalog URL Rewrites the following error occurs:
"An error occurred while saving the URL rewrite"

Attempts to fix:

  1. recursively chown entire store directory to correct user
  2. recursively chmod 777 var and media
  3. clear var/session, var/cache, var/locks
  4. Backup and truncate core_url_rewrite (resolves or a short time) but less than ideal due to indexing issues on renamed products and future use of custom rewrites

Logs:
Checking the logs the errors that are being generated are along the following lines:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'category-name/sub-category-1' for key 'UNQ_CORE_URL_REWRITE_REQUEST_PATH_STORE_ID'

Database:
UNQ_CORE_URL_REWRITE_REQUEST_PATH_STORE_ID belongs to core_url_rewrite and is a UNIQUE KEY across the store_id and request_path.

The duplicate value in question doesn't actually exist in the request_path however multiple instances of request_path/store_id duplicates seem to be in the database.

Example of a duplicate entry:

INSERT INTO `core_url_rewrite` (`url_rewrite_id`, `store_id`, `category_id`, `product_id`, `id_path`, `request_path`, `target_path`, `is_system`, `options`, `description`)
VALUES
    (5246, 1, 376, NULL, 'category/376', 'test-assemblies/123-assemblies', 'catalog/category/view/id/376', 1, NULL, NULL),
    (6334, 1, 376, NULL, 'category/376', 'test-assemblies/123-assemblies', 'catalog/category/view/id/376', 1, NULL, NULL);

Questions

  1. Would it be safe for me to go through and remove all duplicate entries and potentially resolve the issue?
  2. What could be happening to allow duplicate entries to occur?
  3. Can anyone shed any light on the issue?

Update: seems to be an issue with categories not product looking further into the issue.

Best Answer

Your questions answered

Would it be safe for me to go through and remove all duplicate entries and potentially resolve the issue?

If you mean edit the DB directly. No. Never. Ever.

What could be happening to allow duplicate entries to occur?

It can be a number of things; but it will naturally happen from time to time.

Can anyone shed any light on the issue?

Done.

The fix

The fix is very simple actually. Where you see the error

Duplicate entry for product ID XXX

  1. Change all indexes in Admin > System > Index Management to be Update-On-Save

  2. Open Admin > Catalogue > Manage Products - filter the grid by product ID above - then edit the product.

  3. Hit save on the product edit page

This usually rectifies 99% of Duplicate entry errors for products. Try and run the mass failed index from before to confirm. You can then change your index actions back to whatever they were previously.