5

I am using the magento version 2.0.9 version for my site.

I am getting this Url key for specified store is already exist while updating the product or assigning the product for custom option and click on Save button.

I have deleted all entries in URL rewrite table in the database also. But I am still getting this error while updating any product.

Any idea for this error?

5

I came here searching for a similar issue but with Category save. So when I tried to save the category it threw same error "URL key for specified store already exists.".

After debugging by removing the try..catch block at insertMultiple() method of Magento\UrlRewrite\Model\Storage\DbStorage. I identified that while saving the category Magento was trying to re-build url rewrites for assigned products also. And few products had same url_key and that was throwing duplicate records error. As url_rewrite table has a UNIQUE contraint on request_path, store_id columns.

See exception.log to find out the url_key that is creating issue.

So I just made the url_key unique for products and that resolved the issue.

Hope these debugging steps may prove helpful to somebody who stumbles here again.

  • This saved me a huge headache!! Thank you. In my case I had a handful of unique products that had identical names that were tossing this issue that I didn't even think about. Also when creating configurable products keep in mind the request_path needs to be unique for each simple product. – Vallier Jun 29 '17 at 15:48
2

In admin Products -> categories

Please check there is a space before "Default Category"

like this:

" Default Category"

click on that and remove space before Default Category after that import you CSV file.

Also Check for different category has same category url

Like this :

Inside a Search Engine Optimization tab change url is it 's same as a different category url

it will work.

1

First find the which table use for product data insert

Second find product which record id zero and replace increment id for record in table Hope it will work for you

1

I faced the same issue and the isue is solved now by truncating the "url_rewrite" table from database.But it is not always a better solution to truncate the table and generate the urls.However the small change in the code in "doReplace" method of Magento\UrlRewrite\Model\Storage\DbStorage will do it for us. As it is a core file I have extended the file in my custom module as below, the di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\UrlRewrite\Model\Storage\DbStorage" type="Custom\UrlRewrite\Model\DbStorage"/>
</config>

then comes our extended file
path:Custom\UrlRewrite\Model\DbStorage.php

<?php
namespace Custom\UrlRewrite\Model;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;

class DbStorage extends \Magento\UrlRewrite\Model\Storage\DbStorage {

    protected function doReplace($urls) {

        foreach ($this->createFilterDataBasedOnUrls($urls) as $type => $urlData) {
            $urlData[UrlRewrite::ENTITY_TYPE] = $type;
            $this->deleteByData($urlData);
        }
        $data = [];
        $storeId_requestPaths = [];
        foreach ($urls as $url) {
            $storeId = $url->getStoreId();
            $requestPath = $url->getRequestPath();
            // Skip if is exist in the database
            $sql = "SELECT * FROM url_rewrite where store_id = $storeId and request_path = '$requestPath'";
            $exists = $this->connection->fetchOne($sql);

            if ($exists)
                continue;

            $storeId_requestPaths[] = $storeId . '-' . $requestPath;
            $data[] = $url->toArray();
        }

        // Remove duplication data;
        $n = count($storeId_requestPaths);
        for ($i = 0; $i < $n - 1; $i++) {
            for ($j = $i + 1; $j < $n; $j++) {
                if ($storeId_requestPaths[$i] == $storeId_requestPaths[$j]) {
                    unset($data[$j]);
                }
            }
        }
        $this->insertMultiple($data);
    }

}

for reference I have got the answer from this post
https://github.com/magento/magento2/issues/7298
hope it helps...

-1

Issue is resolved by deleting the entries from "url_rewrite" table from database..After deleting the entries from table,then assign category to the products and click on save button.

You can easily assign the categories to the products.

  • As Spyrule says above, do NOT do this (without a backup)! Category URLs will not be rebuilt if you remove them from the url_rewrite table. – James Jul 6 '17 at 8:27
-1

I found that in addition to clearing any entries in the URL_rewrite I found that any indexing must be completed as well. BTW: I limited the deletion as follows:

Delete from url_rewrite where entity_type = 'category'

  • 1
    Do NOT delete url_rewrite unless you want all of your menu's to no longer work. Re-indexing does not rebuild these urls, so you manually have to re-save them to get the url paths to work again, plus any categories that are having this error, will still have them but now won't be able to be re-saved. I tried this, and had to perform a DB restore to undo this simple "fix" – Spyrule Jan 31 '17 at 18:10
  • Well If you reindex your menu will not break, just the url's will not be nicely formatted – Stevie G Feb 17 '17 at 9:41
  • didn't work for me. – Vladimir Despotovic Apr 20 '17 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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