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...