Magento2: URL key for specified store already exists 2.1.6

Created on 18 Apr 2017  路  20Comments  路  Source: magento/magento2


Preconditions


  1. Magento version 2.1.6
  2. With needed prerequisites.

Steps to reproduce

  1. Our website was running with Magento version 2.0.5 and then we have decided to upgrade to latest Magento version(2.1.6).
  2. We have created some attributes in 2.0.5 programmatically which we needed and we have custom build theme and created a store for our purpose.
  3. When we assign the existing or new products to the Categories in the manage category page in the admin end or when we assign the categories to the product in the admin->manage product page.

Expected result

  1. The product and category should be saved

Actual result

  1. [Screenshot, logs]

capture

Format is valid non-issue

Most helpful comment

This is a known bug (feature?) of the URL Rewrite functionality. See https://github.com/magento/magento2/issues/6671 for reference. The table url_rewrite has a UNIQUE index on columns request_path and store_id. This means that the url keys cannot be duplicated at the database level.

The simplest way to work around it is to ensure that your categories and products have unique names when saving them. What I ended up doing is I wrote a plugin for the URL Rewrite functionality that checks for an AlreadyExistsException on save. If there is an Exception, it generates and saves new URL key with an appended number, _脿 la_ WordPress (ex:myurlkey-1). It's not pretty, but it avoids having to deal with this nasty bug.

All 20 comments

Try to check field 'url_key' of categories or products. If it's duplicated with others, it will return this error.

No the URL key is unique for the categories and for the products, it is working in earlier magento version 2.0.5 but after upgrade 2.1.6 it is not working.

Yes the URL key must be unique. but I found this problem since Magento 2.1.2. So I use this way to work around.

@magento-team Could you help to solve this problem

This is a known bug (feature?) of the URL Rewrite functionality. See https://github.com/magento/magento2/issues/6671 for reference. The table url_rewrite has a UNIQUE index on columns request_path and store_id. This means that the url keys cannot be duplicated at the database level.

The simplest way to work around it is to ensure that your categories and products have unique names when saving them. What I ended up doing is I wrote a plugin for the URL Rewrite functionality that checks for an AlreadyExistsException on save. If there is an Exception, it generates and saves new URL key with an appended number, _脿 la_ WordPress (ex:myurlkey-1). It's not pretty, but it avoids having to deal with this nasty bug.

This issue can be fixed by changing the flow of saving the categories, ie first deselect(Default category) the anchor set to no and save the category. Then associate the products to children categories and save it then save the parent category with set anchor yes and save, it works fine for me

This Magento 2 bug you can directy fix is changing file

File Path :vendor/magento/module-url-rewrite/Model/Storage/DbStorage.php

Orginal Code:

from

protected function doReplace($urls)
{
    foreach ($this->createFilterDataBasedOnUrls($urls) as $type => $urlData) {
        $urlData[UrlRewrite::ENTITY_TYPE] = $type;
        $this->deleteByData($urlData);
    }
    $data = [];
    foreach ($urls as $url) {
        $data[] = $url->toArray();
    }
    $this->insertMultiple($data);
}

into

protected function doReplace($urls)
{
    foreach ($this->createFilterDataBasedOnUrls($urls) as $type => $urlData) {
        $urlData[UrlRewrite::ENTITY_TYPE] = $type;
        $this->deleteByData($urlData);
    }
    $data = [];
    foreach ($urls as $url) {
        $data[] = $url->toArray();
    }

     /* Add this line : Get rid of rewrite for root Magento category to unduplicate things
    foreach($data as $key =>$info){
            if(isset($info['target_path']) && stristr($info['target_path'],'/category/1') && $info['entity_type']=='product'){
                    unset($data[$key]);
            }
    }

    $this->insertMultiple($data);
}

After insert this line clear cache.

@sadeeshmca why to spam in a bunch of issues? ;) See https://github.com/magento/magento2/blob/develop/CONTRIBUTING.md

Not sure if it's related but it took reading through a couple issues to figure out what was going on for my issue. A couple weeks ago we upgraded from 2.0.10 to 2.1.6 and soon after switched to flat products and categories. Before then we were not having any issues saving our categories.

Yesterday I was making a couple changes to the meta data for a category and tried to save, got the URL key for specified store already exists message. After some research and running a query on the flat product table, I found a couple products had duplicate entries. These products were imported back in 2.0.7 originally using the system->import feature. They were added again when running the import process to add images to the products. Again, using the system->import feature. Sku, name, and yet for some reason had null URL paths. I deleted the original product entry, reran indexers, cleared cache and was able to save the category.

So, it seems similar to the situation that @niranjanleanswift had in adding attributes to existing products. Not sure if it is exactly related but hope to add some info to the discussion.

screen shot 2017-05-21 at 2 45 34 pm

0
down vote

I was also facing same issue while adding/editing categories.

Firstly I found there was wrong attribute id in "catalog_category_entity_varchar" table for url_key and url_path. Then I checked the exect attribute_id of both attributes ('url_key' and 'url_path').

e.g in "catalog_category_entity_varchar" table it was showing '117' and '118' but exact id was '119' and '120'. Then I found there was some rows in "catalog_category_entity_varchar" table with these (correct id '119','120').

I followed below steps to resolve issue.

firstly I searched rows in "catalog_category_entity_varchar" where attribute id is in (117,118) and exported the data from table.
then I searched rows in "catalog_category_entity_varchar" where attribute id is in (117,118,119,120) and deleted these data (rows) from table.
then I just opened exported file in notpadd++ then I changed attribute ids 117 with 119 and 119 with 120 and import the data again.

Make sure take the backup of the database before doing above steps.

I encountered this multiple times and have a set of tools that fix it. Contact me if you need it.

Hi guys,

Let's try my solutions issue #7298.

Hope this help.
Thanks.

System works as expected with the steps described.

Error message was improved on Magento 2.2.0 branch to give more information to merchant what exactly needs to be changed.

@orlangur I understand that the new error messages in 2.2.0 inform the users which url keys are clashing. Is there any chance you could post am equivilent sql query so that people can correct this themselves? Im getting this issue pretty much every time I try to duplicate a product. I duplicate, try to edit name and url key and get this error. The new url key is definitely unique so Im wondering what is clashing.

Hi @ahumphreys87, applying some raw SQL queries manually does not seem to be a normal workflow to me, if you just remove some conflicting URL rewrites of previously created entities you will make them inaccessible with pretty URL in corresponding stores.

Please report duplication problem targeting 2.2.0 as it does not sound good to me that you have to edit some fields manually EACH time you duplicate a product. I don't remember if it always worked like that though in 2.0 GA+.

Hi @orlangur, I dont mean to remove the items in the DB. I just want to know which items are clashing so I can update them via admin.

I suspect my issues are due to data migration from v1.9. I will open a new issue for duplication.

@ahumphreys87 Did you end up finding a solution for identifying these without being on 2.2.0? We are waiting for 2.2.1 as I was not pleased with a few issues I saw crop up immediately with that release and are experiencing this exact issue from our v1-v2 migration data.

The Strange thing is Magento2 allows you to enter duplicate url-key from products. There is no any checks.

@ahumphreys87 could you link the relevant issue please?
@J-Fricke 2.2.1 did not help (us).
@orlangur I opt for a debugging sql-query, too. Its possible that a messy migration lead the db into a stage that should not have occurred. And of course the issue itself should be fixed, but having the SQL query at hand could help debugging (and thus fixing the issue within the next years).

I was looking for solution for this problem and i allready found. I created short movie with doc to fix this problem. https://www.youtube.com/watch?v=xerkbeiH2jU

Step by step instruction

Was this page helpful?
0 / 5 - 0 ratings