I've been trying to update/save a specific category but get the message "URL key for specified store already exists." every time.
I don't know if you can replicated it as it's very specific.
Message: _URL key for specified store already exists._ comes up and nothing is saved
Magento ver. 2.1.2
PHP 7
MySQL 5.6
url_rewrite
and found no duplicates.delete from url_rewrite where entity_type='product' and entity_id NOT IN (Select entity_id from catalog_product_entity);
select count(*) from url_rewrite where entity_type='product' and entity_id NOT IN (Select entity_id from catalog_product_entity);
Each time I've re-ran the indexer via ssh and cleared all caches, nothing has worked.
Any ideas?
@janeblonde :
That issue because the table catalog_product_entity_varchar
has duplicate values of (store_id, value) with attributes: url_key (119) vs url_path (120) See more about these attributes: http://i.prntscr.com/8604a114212f4661a8f52b48576fe6f0.png
Let's run below SQL in your Magento 2 database, you will see the duplicated values:
SELECT * FROM `catalog_product_entity_varchar` WHERE `attribute_id` IN (119, 120) AND `value_id` NOT IN (
SELECT `value_id` FROM `catalog_product_entity_varchar` WHERE `attribute_id` IN (119, 120) GROUP BY `store_id`, `value`
)
After run that sql, if you see any rows in result, let's find way to delete that records in the table catalog_product_entity_varchar
And then the issue will be solved.
Hope this help.
Regards,
Quynhvv
@quynhvv What would be the implication of delete entries from catalog_product_entity_varchar
?
@frostshoxx : Let's run below SQL in your database, you will see the records which you have to delete.
SELECT * FROM
catalog_product_entity_varchar
WHEREattribute_id
IN (119, 120) ANDvalue_id
NOT IN (
SELECTvalue_id
FROMcatalog_product_entity_varchar
WHEREattribute_id
IN (119, 120) GROUP BYstore_id
,value
)
There's actually nothing return as a result.
@frostshoxx : So, what happened with below SQLs:
SELECT count(value_id) FROM catalog_product_entity_varchar WHERE attribute_id IN (119, 120) GROUP BY store_id, value
vs
SELECT count(value_id) FROM catalog_product_entity_varchar WHERE attribute_id IN (119, 120)
@quynhvv the first entry return empty, the 2nd one return 0. Perhaps my store attribute_id for url_key and url_path are not 119, 120?
I'm closing this issue as a duplicate of #6671 .
Actually, I think that there are multiple issues here. #6671 has narrowed down to an anchor URL rewrite generator. I suspect that this issue is related but seperate.
Hi guys, in my case I received a bucket load of this issue.
Steps to reproduce my findings:
Result:
Don't you know it, the new url_key "item-a-1" gets the (Specified URL key already exists) error again.
@quynhvv Thanks for the proposed solution. However, I tried your query, returns 0 rows.
Same issue here - using the data migration tool from an M1 CE store, cannot save any categories.
I also encountered this issue after I cleaned the magento database.
At last I found the sql script I used was incomplete and some data still remained in database.
https://blog.mdnsolutions.com/magento-2-delete-all-categories/
I used the script suggested in this blog and the problem was solved.
Hi,
In my case, the problem was caused by the interruption of a product import. I just needed to remove all the products that had been imported and after that I was able to save the category. The other solutions proposed in this and other tickets didn't work.
@PauGNU Just FYI for others, this did not work in my case.
it seems something to do with Unicode characters, @zaknixon do you have Unicode characters in your category names? I'm facing the same issue. I suspect my Unicode text on category name ruined auto generated url key and set it null or Unicode characters instead.
Just an FYI I have had this error come up repeatedly as I have uploaded products. Believe it or not the problem is always the same thing. The category cell length can't exceed 250 characters. Any longer and the error appears. I simply duplicate my row and cut the category in half. First half of categories in the first row and the second half in the next one. Then everything uploads fine.
@ezprod would you mind sharing the SQL queries that narrowed down the affected products?
Magento CE 2.1.0, unable to edit/save some existing product categories!
SELECT * FROM
catalog_product_entity_varcharWHERE
attribute_idIN (119, 120) AND
value_idNOT IN (
SELECT
value_idFROM
catalog_product_entity_varcharWHERE
attribute_idIN (119, 120) GROUP BY
store_id,
value
)
returns nothing
SELECT count(value_id) FROM catalog_product_entity_varchar WHERE attribute_id IN (119, 120) GROUP BY store_id, value
gives me 568 total
SELECT count(value_id) FROM catalog_product_entity_varchar WHERE attribute_id IN (119, 120)
gives me 573
What should I go from here?
I believe in
catalog_category_entity_varchar
Varchar is limited to 255. It still utilizes this format for categories even though it breaks it into multiple cells as it reads the poles or commas in the import.
This Magento 2 bug you can directy fix is changing file
File Path :vendor/magento/module-url-rewrite/Model/Storage/DbStorage.php
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 I tried your fix but it didn't work for me as the error persists as I tried to save my category. I couldn't find your suggested file, in my case I edited:
app/code/Magento/UrlRewrite/Model/Storage/DbStorage.php
I have fixed some of these problematic categories by exclude certain products, it looks like those products were created wrong, maybe they been duplicated without changing the url key? I never had such issue until I handed over to the client when they started messing around with duplicate products.
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.
In my case it was enough to set is_anchor=0 for root magento category (entity_id=1)
I have struggled with this problem for almost a day.
And finnally i found out about this.
It is because of my data migrated from magento V1 to magento V2.
Some categories have the same url as products. And when we save categories. It throws mysql exception named URL_REWRITE_REQUEST_PATH_STORE_ID (request_path, store_id is unique)
There are my solutions:
The firt one : Clean up your database in table url_rewrite (Change the url_key of all category). You can write UpgradeData script for this solution.
The second one : Remove the duplication data when saving category.
This data is throw in method doReplace($urls) in \vendor\magento\module-url-rewrite\Model\Storage\DbStorage.php file.
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);
}
After debugging, i found out $data variable have duplicate record. If you want this method to work without any errors. Rewrite this method above to:
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);
}
These solutions above both work for me. But the second one is not an ideal solution.
Because, there are wrong url_keys in your database.
And sometime, it will not work as you expected in the front end.
I hope it will help.
Thanks.
Toan Tam
@toanthree I like your second option, works great for me, I only added the last line of the old function, that makes it work for real:
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->getRerquestPath();
$url->setRequestPath($requestPath);
// 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]);
}
}
}
// remove root links
foreach( $data as $key => $info ){
if( isset($info['target_path']) && ( stristr($info['target_path'],'/category/1') || stristr($info['target_path'],'/category/2') ) && $info['entity_type']=='product' || $info['request_path'] == "" ){
unset($data[$key]);
}
}
// create links
if( count($data) > 0 ){
// file_put_contents('var/log/url_rewite.log', print_r($data, TRUE), FILE_APPEND); // enable to log
$this->insertMultiple($data);
}
}
@toanthree @scher200 guys, as far as I understood an implementation it is not a solution at all.
By skipping some request paths it simply makes products inaccessible in some stores using their url_key
(so, URL Rewrite for such products will not be created and its URL in listing will be something like /catalog/product/view/id/123
instead of proper one).
Please tell me if my observation is wrong as I didn't try this code in action.
@orlangur thanks again, this code inspired me for the posted code and that worked well.
You just forgot the "//create links" part
Are you removing previously stored conflicting URL Rewrites in this code snippet?
@orlangur Like i said, the second solution is not an ideal one. But it help fixing the problem when migrate data from Magento V1 to Magento V2 (in my case).
Sure thing 馃憤 Just want to be sure we are on the same page.
So, such code may lead to partial data loss, but until it's under control (for example, to dump which conflicting records were removed from database) it is totally fine.
@orlangur Yeah. In my case, it was the category url rewrite and product url rewrite causing the problem (Some of its url is duplicate).
My first solution was change the category url, and it worked pretty well.
But our customer didn't want to change thier url (because of the seo thing).
After that, i had to debugging and digging into the core code.
Finnally i found the solution above, and it worked well, too.
Note: Just some of the url of product won't work. because i only remove those url which is duplicate with others.
ARGHH. Trying to put a fix in place with a preference on ProductScopeRewriteGenerator but every **ing thing is private. Magento, PLEASE STOP DOING THIS!
@maderlock this is one of the most right design decisions in Magento 2, see http://fabien.potencier.org/pragmatism-over-theory-protected-vs-private.html.
@orlangur
see http://fabien.potencier.org/pragmatism-over-theory-protected-vs-private.html
Closing the API allows design flaws to be found more easily
That's all nice and well _as long as core developers fix the design flaws in a timely matter_. Otherwise the developer will need to hack into the core in order to perform the changes necessary.
I mean here we discuss an issue that's almost a year old and still the best solutions they are will require you to hack into the core yourselves.
In such case these private
markers turn out to be nothing more than a large PITA for the person who has to deal with it on a real project.
Unless bugfixes will be delivered in a timely matter and there are more extension points provided, those private
markers are more of an issue than a solution.
I have this same issue with a brand new install. From the very first product and/or category save in 2.1.5 EE along with Asymmetric Transaction Rollback constantly too (yes I've indexed! only hacking core to remove indexer works - again with NO products or customers at all). Been struggling with a generic install with no modifications, and this is after messing with M2 development for over a year and on different platforms (and 6+ years experience with M1).
For the query to find duplicates:
SELECT value, attribute_id, store_id, COUNT(*) FROM 'catalog_product_entity_varchar' WHERE 'attribute_id' IN (122,124) GROUP BY value, attribute_id, store_id HAVING COUNT(*) > 1
X should be replaced by the first url_key attribute, and Y with the second (there should be two: one each for category and product).
any solution?
@scher200 you will save my life if you explain the loop and tell me purpose of the if statement:
// remove root links
foreach( $data as $key => $info ){
if( isset($info['target_path']) && ( stristr($info['target_path'],'/category/1') || stristr($info['target_path'],'/category/2') ) && $info['entity_type']=='product' || $info['request_path'] == "" ){
unset($data[$key]);
}
}
// create links
if( count($data) > 0 ){
// file_put_contents('var/log/url_rewite.log', print_r($data, TRUE), FILE_APPEND); // enable to log
$this->insertMultiple($data);
}
I just want to add one more reason why I received the url key error. In addition to fixing the url key product attributes after a data migration I ALSO had to delete any orphan records in url_rewrite :
delete from url_rewrite where entity_type = 'product' and entity_id not in (select entity_id from catalog_product_entity);
Hope this helps someone. Thank you all for sharing your solutions!
what we just discovered is this:
we had custom-redirects for category-urls as a means of redirecting traffic for disabled categories.
when we reenabled the categories the custom entries remained and magento is "blind" and doesnt recognize them and promptly walks into the dreaded duplicate key error.
@scher200 it is minor typo at getRerquestPath
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
@sadeeshmca @scher200 @umarch06 Can you please tell me, What is '1' in below which is used in our custom function
'/category/1'
root Magento category
@NiteshKuyate
Are those workarounds still need in 2.2.x ?
Looks like it should be all fixed: https://github.com/magento/magento2/issues/6671#issuecomment-330999486
Still an issue for me, version 2.2.5
( I get the message when trying to edit 'content manager > content lists' )
Edit: fixed by deleting the offending 'contenttype_contentlist' from the 'url_rewrite' table, then clicking 'save' again in admin, which worked, and created new entry in 'url_rewrite' table.
Can still reproduce this in 2.2.6 and 2.3. The solution by @toanthree is working for me. We will create a PR, if @toanthree does not want to do it (?).
Hi @josefbehr. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
Issue: Format is valid
will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid
appears.[ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description
label to the issue by yourself.
[x] 3. Add Component: XXXXX
label(s) to the ticket, indicating the components it may be related to.
[x] 4. Verify that the issue is reproducible on 2.3-develop
branchDetails
- Add the comment @magento-engcom-team give me 2.3-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop
branch, please, add the label Reproduced on 2.3.x
.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[x] 5. Verify that the issue is reproducible on 2.2-develop
branch. Details
- Add the comment @magento-engcom-team give me 2.2-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.2-develop
branch, please add the label Reproduced on 2.2.x
[ ] 6. Add label Issue: Confirmed
once verification is complete.
[ ] 7. Make sure that automatic system confirms that report has been added to the backlog.
@josefbehr I'll make a PR and add you as reviewer.
This is still a nightmare issue in 2.1.16
It seems several things can cause this duplicate condition.... importing of data of course as mentioned already and also using the "Save & Duplicate" feature on a product. Magento allows url key to be duplicated at times and if this happens only once on a single product.... then every product added after that will not get a proper url key rewrite... it silently fails at that point when creating new products and destroys SEO as all those newly added products do not have the friendly url.
Only after manually finding the duplicates in the product table and correcting them will it start working again for many products. However it will then also fall back to another instance of the category rewrite bug and will stick on a certain product requiring furthers manual digging in the database and hacks.
To then go fix the remaining bugged products I have to set single store mode and then they will save. Fortunately my client only has one store at the moment, however future plans are for multi store mode.
any update on this? this is a crazy issue with no definite solution in magento. why does magento have so many bugs still not fixed correctly. pfff
@veloraven I see you everywhere just closing the threads and doing nothing else. could you not do that please until it is definitely fixed.
I have seen this behaviour on a 2.2.6 install too.
@toanthree Your solution appears to resolve the issue locally. However, the solution makes use of the method "createFilterDataBasedOnUrls" which is marked as deprecated. Is there any workaround for this? Do we know when the method will be removed?
Hello,
I got also the same issue in Magento 2.2.4 after data migration from Magento1.9.1.0.
I have checked file
vendor/magento/module-url-rewrite/Model/Storage/DbStorage.php
and found that in function doReplace(array $urls) ,$this->deleteOldUrls($urls); is not working properly.
So I have resolve this issue with help of forums
//$this->deleteOldUrls($urls);
//~ $data = [];
//~ foreach ($urls as $url) {
//~ $data[] = $url->toArray();
//~ }
/*Custom Functinality Used for delete old Url ,we need to override it*/
$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]);
}
}
}
Now issue is resolved and working fine.
After a migration and a week digging into the problem the only thing that worked for me was https://www.safemage.com/url-optimization-after-migration-magento-2.html
I had to downgrade to 2.2.7 to use it. It says it works on 2.3 but it does not.
Fix for me on a single store view, was to remove anything that had store_id as 1 set from importing data.
DELETE FROM catalog_product_entity_varchar
WHERE store_id
= 1
@kestraly Are you sure you wanted to delete every strings in store_id ? Very dangerous query :)
@amenk - Yeah. Backed it up first. A resaved doesn't create anything for that store view. No other issues experienced. So yeah, I did intend to do that and the consequences were none existent apart from fixing the issue. Next!
@sadeeshmca
in latest magento 2.3 code totally change in this function
protected function doReplace(array $urls)
{
$this->deleteOldUrls($urls);
$data = [];
foreach ($urls as $url) {
$data[] = $url->toArray();
}
try {
$this->insertMultiple($data);
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urlConflicted */
$urlConflicted = [];
foreach ($urls as $url) {
$urlFound = $this->doFindOneByData(
[
UrlRewrite::REQUEST_PATH => $url->getRequestPath(),
UrlRewrite::STORE_ID => $url->getStoreId(),
]
);
if (isset($urlFound[UrlRewrite::URL_REWRITE_ID])) {
$urlConflicted[$urlFound[UrlRewrite::URL_REWRITE_ID]] = $url->toArray();
}
}
if ($urlConflicted) {
throw new \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException(
__('URL key for specified store already exists.'),
$e,
$e->getCode(),
$urlConflicted
);
} else {
throw $e->getPrevious() ?: $e;
}
}
return $urls;
}
Then how can replace this function ?
Although this doesn't fix the underlying bug(s) in Magento, running this CLI command can help mitigate it.
https://github.com/elgentos/regenerate-catalog-urls
@jaywilliams i have use your suggest extension but after url generate and my database size full and product page gone 404 error page
You can fix the issue as you DO NOT click the "Use Default" in Search Engine Optimization.
Does the issue still persist in 2.3?
Yes magento 2.3.2 has also the same issues
@petefok Can you explain this please?
I am no longer having the issue in 2.3.2 as they seem to have built in some GUI mechanism that notifies you about conflicting urls, and gives you the option then to go and delete it from url rewrites. Also, if you don't care about redirecting from old url to new one, you can simply clear the database of rewrites and then import your product file, which will then generate the urls from scratch. Hope that helps
Hello I am facing same issue in 2.3.2 after migration
main.CRITICAL: Unique constraint violation found {"exception":"[object] (Magento\\Framework\\Exception\\AlreadyExistsException(code: 0): Unique constraint violation found at /vendor/magento/framework/EntityManager/Operation/Update.php:121, Magento\\Framework\\DB\\Adapter\\DuplicateException(code: 1062): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'White-Ultra-Board-Custom-Cut-Sizes.html-1' for key 'MGL0_URL_REWRITE_REQUEST_PATH_STORE_ID', query was: INSERT INTO
mgl0_url_rewrite(
redirect_type,
is_autogenerated,
metadata,
description,
entity_type,
entity_id,
request_path,
target_path,
store_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?) at /vendor/magento/framework/DB/Adapter/Pdo/Mysql.php:589, Zend_Db_Statement_Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'White-Ultra-Board-Custom-Cut-Sizes.html-1' for key 'MGL0_URL_REWRITE_REQUEST_PATH_STORE_ID', query was: INSERT INTO
mgl0_url_rewrite(
redirect_type,
is_autogenerated,
metadata,
description,
entity_type,
entity_id,
request_path,
target_path,
store_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?) at ...]
Any idea to how to resolve.
I tried to delete all product url in url_rewrite
table. after save product I am facing Unique constraint violation found
.
Hello I am facing same issue in 2.3.2 after migration
main.CRITICAL: Unique constraint violation found {"exception":"[object] (Magento\\Framework\\Exception\\AlreadyExistsException(code: 0): Unique constraint violation found at /vendor/magento/framework/EntityManager/Operation/Update.php:121, Magento\\Framework\\DB\\Adapter\\DuplicateException(code: 1062): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'White-Ultra-Board-Custom-Cut-Sizes.html-1' for key 'MGL0_URL_REWRITE_REQUEST_PATH_STORE_ID', query was: INSERT INTO
mgl0_url_rewrite(
redirect_type,
is_autogenerated,
metadata,
description,
entity_type,
entity_id,
request_path,
target_path,
store_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?) at /vendor/magento/framework/DB/Adapter/Pdo/Mysql.php:589, Zend_Db_Statement_Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'White-Ultra-Board-Custom-Cut-Sizes.html-1' for key 'MGL0_URL_REWRITE_REQUEST_PATH_STORE_ID', query was: INSERT INTO
mgl0_url_rewrite(
redirect_type,
is_autogenerated,
metadata,
description,
entity_type,
entity_id,
request_path,
target_path,
store_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?) at /vendor/magento/framework/DB/Statement/Pdo/Mysql.php:110, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'White-Ultra-Board-Custom-Cut-Sizes.html-1' for key 'MGL0_URL_REWRITE_REQUEST_PATH_STORE_ID' at /vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91)"} [] [2019-08-29 11:15:46] main.CRITICAL: Exception message: Unique constraint violation found Trace: <pre>#1 Magento\Framework\EntityManager\EntityManager->save(&Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#) called at [vendor/magento/module-catalog/Model/ResourceModel/Product.php:680] #2 Magento\Catalog\Model\ResourceModel\Product->save(&Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#) called at [vendor/magento/framework/Interception/Interceptor.php:58] #3 Magento\Catalog\Model\ResourceModel\Product\Interceptor->___callParent('save', array(&Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#)) called at [vendor/magento/framework/Interception/Interceptor.php:138] #4 Magento\Catalog\Model\ResourceModel\Product\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#) called at [vendor/magento/module-catalog-search/Model/Indexer/Fulltext/Plugin/Product.php:58] #5 Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product->addCommitCallback(&Magento\Catalog\Model\ResourceModel\Product\Interceptor#0000000065b4360f0000000061455766#, &Closure#0000000065b434160000000061455766#, &Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#) called at [vendor/magento/module-catalog-search/Model/Indexer/Fulltext/Plugin/Product.php:28] #6 Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product->aroundSave(&Magento\Catalog\Model\ResourceModel\Product\Interceptor#0000000065b4360f0000000061455766#, &Closure#0000000065b434160000000061455766#, &Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#) called at [vendor/magento/framework/Interception/Interceptor.php:135] #7 Magento\Catalog\Model\ResourceModel\Product\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#) called at [vendor/magento/framework/App/Cache/FlushCacheByTags.php:68] #8 Magento\Framework\App\Cache\FlushCacheByTags->aroundSave(&Magento\Catalog\Model\ResourceModel\Product\Interceptor#0000000065b4360f0000000061455766#, &Closure#0000000065b434160000000061455766#, &Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#) called at [vendor/magento/framework/Interception/Interceptor.php:135] #9 Magento\Catalog\Model\ResourceModel\Product\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#) called at [vendor/magento/framework/Interception/Interceptor.php:153] #10 Magento\Catalog\Model\ResourceModel\Product\Interceptor->___callPlugins('save', array(&Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#), array(array('apply_catalog_ru...'), array('reload_attribute...'))) called at [generated/code/Magento/Catalog/Model/ResourceModel/Product/Interceptor.php:273] #11 Magento\Catalog\Model\ResourceModel\Product\Interceptor->save(&Magento\Catalog\Model\Product\Interceptor#0000000065b434430000000061455766#) called at [vendor/magento/framework/Model/AbstractModel.php:655] #12 Magento\Framework\Model\AbstractModel->save() called at [generated/code/Magento/Catalog/Model/Product/Interceptor.php:2442] #13 Magento\Catalog\Model\Product\Interceptor->save() called at [vendor/magento/module-catalog/Controller/Adminhtml/Product/Save.php:131] #14 Magento\Catalog\Controller\Adminhtml\Product\Save->execute() called at [generated/code/Magento/Catalog/Controller/Adminhtml/Product/Save/Interceptor.php:24] #15 Magento\Catalog\Controller\Adminhtml\Product\Save\Interceptor->execute() called at [vendor/magento/framework/App/Action/Action.php:108] #16 Magento\Framework\App\Action\Action->dispatch(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#) called at [vendor/magento/module-backend/App/AbstractAction.php:231] #17 Magento\Backend\App\AbstractAction->dispatch(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#) called at [vendor/magento/framework/Interception/Interceptor.php:58] #18 Magento\Catalog\Controller\Adminhtml\Product\Save\Interceptor->___callParent('dispatch', array(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#)) called at [vendor/magento/framework/Interception/Interceptor.php:138] #19 Magento\Catalog\Controller\Adminhtml\Product\Save\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#) called at [vendor/magento/module-backend/App/Action/Plugin/Authentication.php:143] #20 Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch(&Magento\Catalog\Controller\Adminhtml\Product\Save\Interceptor#0000000065b4364b0000000061455766#, &Closure#0000000065b436770000000061455766#, &Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#) called at [vendor/magento/framework/Interception/Interceptor.php:135] #21 Magento\Catalog\Controller\Adminhtml\Product\Save\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#) called at [vendor/magento/framework/Interception/Interceptor.php:153] #22 Magento\Catalog\Controller\Adminhtml\Product\Save\Interceptor->___callPlugins('dispatch', array(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#), NULL) called at [generated/code/Magento/Catalog/Controller/Adminhtml/Product/Save/Interceptor.php:39] #23 Magento\Catalog\Controller\Adminhtml\Product\Save\Interceptor->dispatch(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#) called at [vendor/magento/framework/App/FrontController.php:159] #24 Magento\Framework\App\FrontController->processRequest(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#, &Magento\Catalog\Controller\Adminhtml\Product\Save\Interceptor#0000000065b4364b0000000061455766#) called at [vendor/magento/framework/App/FrontController.php:99] #25 Magento\Framework\App\FrontController->dispatch(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#) called at [vendor/magento/framework/Interception/Interceptor.php:58] #26 Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', array(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#)) called at [vendor/magento/framework/Interception/Interceptor.php:138] #27 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#) called at [vendor/magento/framework/Interception/Interceptor.php:153] #28 Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', array(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#), array(array('default_store_se...', 'page_cache_from_...', 'storeCookieValid...', 'install', 'configHash'))) called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:26] #29 Magento\Framework\App\FrontController\Interceptor->dispatch(&Magento\Framework\App\Request\Http#0000000065b432a00000000061455766#) called at [vendor/magento/framework/App/Http.php:137] #30 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:24] #31 Magento\Framework\App\Http\Interceptor->launch() called at [vendor/magento/framework/App/Bootstrap.php:261] #32 Magento\Framework\App\Bootstrap->run(&Magento\Framework\App\Http\Interceptor#0000000065b432dd0000000061455766#) called at [index.php:39]
Any idea to how to resolve.
I tried to delete all product url in
url_rewrite
table. after save product I am facingUnique constraint violation found
.
Try this command in your SSH with putty ow whatever tool you prefer.
Do not forget to check the language codes in the deploy of static contend.
And check the commands for your needs if there are things you do not want to happen get them out.
this worked for me, after my error like above was gone
If not check all file and folder permissions and re-run the below
rm -rf var/di/; rm -rf pub/static/; rm -rf var/cache/; rm -rf var/pagecache/; rm -rf generated/; rm -rf var/dir/; rm -rf var/view_preprocessed/*; php bin/magento cache:flush && php bin/magento setup:upgrade && php bin/magento setup:di:compile && php bin/magento setup:static-content:deploy en_US nl_NL && php bin/magento indexer:reindex && php bin/magento cache:enable
Best regards
@koopjesboom, try to avoid giving advise like running rm -rf pub/static/
This will also remove the file pub/static/.htaccess
which you certainly do not want to remove when using Apache as your webserver.
Please use the command rm -r pub/static/*
instead, the *
will not match files which start with a .
in the filename.
Also try to avoid using the flag -f
of the rm
command as much as possible, this should not be needed in any of these commands if your permissions are setup correctly.
Also removing var/di/
and generated/
is very inconsistent, since the first directory is from Magento 2.1 and no longer exists since 2.2 while the second directory only got introduced in 2.2.
Also, there is no directory var/dir/
in Magento.
Giving advise like this and people just copy/pasting stuff like this might make the problem worse instead of improving it, so please try to pay a bit more attention while posting such a long string of commands 馃槈
Also your advise won't help with fixing this database error @visahardik was running against.
(reply on a reaction|)
You are totally right:
"Giving advise like this and people just copy/pasting stuff like this might make the problem worse instead of improving it, so please try to pay a bit more attention while posting such a long string of commands "
It is there:
"And check the commands for your needs if there are things you do not want to happen get them out."
Also your advise won't help with fixing this database error @visahardik was running against.
Oke you try:
Please give him the proper answer and detailed instructions here how to resolve his problem.
This will not only help him but a lot of other people that are now wasting money on developers.
Most of the time the solution is rather simple and problems are solved fast if the right developer is there. But that is a needle in a haystack these days. Yes they are there not to help but to rob you.
But it is the experience I had with the same problem he has.
So i only share What i see happening in my environment and trying to help.
Not to mention a lot of developers are reading his problem ad keep the solution for themselves.
Or give him a private offer because you have to protect your industry at all cost and never take less for an answer. In the end it is all about the money.
@koopjesboom this is not the place for a personal crusade! Also, @hostep is totally right in that your "advice" is not only unhelpful and does NOT solve the problem at hand, but also is potentially even more damaging to the system if taken seriously. Please refrain from giving advice, if you do not know what you are doing.
@sidolov @maghamed @okorshenko is there any effort to resolve this issue internally? Otherwise, community will have to try and find a solution, finally.
@josefbehr
You are right do not take it personal I did not write this to any person but to all developers watching this post.
I will remove @hostep for you.
Feel free to post the solution here if available.
Hi @VladimirZaets. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
Issue: Format is valid
will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid
appears.[ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description
label to the issue by yourself.
[ ] 3. Add Component: XXXXX
label(s) to the ticket, indicating the components it may be related to.
[ ] 4. Verify that the issue is reproducible on 2.3-develop
branchDetails
- Add the comment @magento give me 2.3-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop
branch, please, add the label Reproduced on 2.3.x
.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 5. Add label Issue: Confirmed
once verification is complete.
[ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
Magento Open Source 2.3.3
PHP 7.2.19
Ubuntu 18.04
Migrated database from 1.9.2.4
Steps to reproduce
Category has URL key of led-deck-lights
Product has URL key of aurora-odyssey-led-strip-light
There is also a URL rewrite with request path/target path
lighting/deck-stair-lights/aurora-odyssey-led-strip-light.html / aurora-odyssey-led-strip-light.html
Purpose of redirect is to redirect a direct category nested link to go directly to the product. Most likely for Canonical/SEO purposes.
When I go to the category in the admin, I can save the category changing some field values like name, etc. If I set Anchor Yes/No, just toggle the switch and save, I get an error that the URL key already exists. I receive the same error if I attempt to remove the single product from the category or if I go to the product and remove the category there, the same exact error.
This product does NOT have a duplicate key. The issue has been narrowed to a URL rewrite. Deleting the URL rewrite fixes the issue, however that is not a workable solution. The company has thousands of URL redirects for varying purposes. URL rewrite management is a valid business function. The raise exception is not logical based on the use case. The raise happens in module-url-rewrite/Model/Storage/DbStorage.php, doReplace()
There are many categories that have this issue and the only solution so far is to delete all url rewrites and not create them, which again is not an acceptable solution.
I have read at least 2 bug reports on this specific issue dating back to 2016. I do not understand why it is still an issue and is not fixed.
I am not sure about all cases but I have determined the cause in our case and it is not necessarily a migration issue.
The business has categories and subcategories. They do not display products under all categories, they use a tile like configuration where a customer will drill down into categories, and eventually a product grid will be shown.
For some odd reason, the business created manual redirects from some of the full category paths to the product page directly, probably for some seo reason.
Example
Direct product url site.com/product.html
category url site.com/category/othercategory/product.html
manual redirect from site.com/category/othercategory/product.html to site.com/product.html
I suspect that in Magento 2, a new functionality has been made to automatically create a url rewrite from *site.com/category/othercategory/product.html to catalog/product/view/id/7550/category/90 - a new type of route
When a product is saved, the error is thrown because there is already a redirect for that request URL. The solution is to remove all such manual redirects - but I still question if this is an actual bug or a data problem and user error. I have pains with Magento design for url rewrite anyway, and often I see companies having to create manual nginx/apache rules because Magento doesn't support standard use cases.
@aliomattux Thank you for the infos! I will try to have a look at this and talk to core developers at tomorrow's contribution day at MagentoLive Europe. Hopefully, we can find a solution.
Hi @engcom-Bravo. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
Issue: Format is valid
will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid
appears.[ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description
label to the issue by yourself.
[ ] 3. Add Component: XXXXX
label(s) to the ticket, indicating the components it may be related to.
[ ] 4. Verify that the issue is reproducible on 2.3-develop
branchDetails
- Add the comment @magento give me 2.3-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop
branch, please, add the label Reproduced on 2.3.x
.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 5. Add label Issue: Confirmed
once verification is complete.
[ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
Hi there. Could anyone please give the exact Steps to Reproduce following which the issue is reproducible on the vanilla 2.3-develop instance. @aliomattux I tried to use the info You've shared and if I understand it right I can reproduce somehow a behavior that gives an error. But that error is "The value specified in the URL Key field would generate a URL that already exists..." and not the one this issue is about, i.e., "URL key for specified store already exists". Besides I'm not sure that behavior isn't an expected one. Though Your explanations are very detailed, probably I still miss or misunderstand something. So could You please write more strict Steps to Reproduce something like this:
1) Create an (Non-)Anchor category "First" with url-key "first".
2) Create a (Non-)Anchor child category for First and name it "Second", url-key - "second".
.............
m) Create product "Prod" with url-key "prod" and assign it to category(ies) First, ...
............
n) Manually create Url Rewrite with Request Path - "...." and Target Path - "...."
...........
etc.
Thank You.
We are closing this issue due to inactivity. If you'd like to update it, please reopen the issue.
still have this issue, this should not be closed
still have the issue also , magento 2.3.3
Still have an issue on 2.3.3
Still have an issue on 2.3.4
Got this issue today on 2.3.4
This might be relevant to this issue:
If you have a parent category with is_anchored
= true
and the product you are trying to remove from the parent exists on a child category this constraint will trigger.
I can't confirm if it exists in all cases, but we are using Elasticsearch which contains the catalog_category_product index and have a "Smart Category" module which handles catalog_category_product index on it's own and whenever a save happens it attempts to check up on url_rewrites. so in our case it might just be this module.
I can consistently make it work by attempting to remove the product from the parent category. however if i go to the child category and remove the product, the exception does not happen. afterwards i can then remove the product from the parent category
Same issue on 2.3.4.
I'm using an external module for categories import and it triggers this error. I'm aware now that it's a Magento2 core issue.
It doesn't seem to be an issue related with products but exclusively with categories.
For people who keep running against this, first start by investigating if you don't have invalid url-related data in your catalog, you can use this module for that if you want to. Then try to fix that invalid data (which you sometimes need to do directly in the database since Magento doesn't have an UI for specific data like product or category url_path's for example).
If you are importing new products or categories, then make sure the url_key's for them won't cause conflicts with already existing categories/products in your shop or with other categories/products in your import data. Everything needs to be unique, otherwise you'll get into troubles!
Hope this helps somehow 馃檪
Magento Open Source 2.3.3
PHP 7.2.19
Ubuntu 18.04
Migrated database from 1.9.2.4Steps to reproduce
Category has URL key of led-deck-lights
Product has URL key of aurora-odyssey-led-strip-light
There is also a URL rewrite with request path/target path
lighting/deck-stair-lights/aurora-odyssey-led-strip-light.html / aurora-odyssey-led-strip-light.htmlPurpose of redirect is to redirect a direct category nested link to go directly to the product. Most likely for Canonical/SEO purposes.
When I go to the category in the admin, I can save the category changing some field values like name, etc. If I set Anchor Yes/No, just toggle the switch and save, I get an error that the URL key already exists. I receive the same error if I attempt to remove the single product from the category or if I go to the product and remove the category there, the same exact error.
This product does NOT have a duplicate key. The issue has been narrowed to a URL rewrite. Deleting the URL rewrite fixes the issue, however that is not a workable solution. The company has thousands of URL redirects for varying purposes. URL rewrite management is a valid business function. The raise exception is not logical based on the use case. The raise happens in module-url-rewrite/Model/Storage/DbStorage.php, doReplace()
There are many categories that have this issue and the only solution so far is to delete all url rewrites and not create them, which again is not an acceptable solution.
I have read at least 2 bug reports on this specific issue dating back to 2016. I do not understand why it is still an issue and is not fixed.
I am also facing this issue unable to understand why this is being happened.
I haven't been following this for a while but did have a quick update. Using Magento 2.3.3
I noticed some strange behavior today. This issue came up again with a product that could not be removed from a category. It listed 8 urls as offending. The product record did not have the url key mentioned, however when I queried the database it had the entry for attribute id = 72 which for me is url path. I also found a disabled product that had the same url key. The disabled product was the one that had the offending 8 urls. It seems that perhaps the url was changed at some point on the active product and only url key was updated, not url path. In the backend/admin it showed url key, but it appears that urls are dynamically generated using url path, not url key. I fixed the issue by changing url path in the database. Unsure if this was caused by a migration issue, or if in the admin there is a problem where saving/updating the url only updates one of the attributes and not the other. But truly, and annoying, agonizing, and frustrating issue.
@aliomattux: in fact you should clear the url_path
for all products in your database. It's a legacy field from Magento 1 which for some reason is still included and being used by Magento 2, even though Magento 2 will never change its contents. It should therefore always be empty otherwise you'll run into problems. One of the reasons why it often isn't empty, is because people migrate from M1 to M2 and inadvertently copy the field over.
I am running into this issues on Magento2.3.5pt-2. Is there any fix for this?
I am running into this issues on Magento2.3.5pt-2. Is there any fix for this?
You can install: https://github.com/baldwin-agency/magento2-module-url-data-integrity-checker to find possible bad categories and products
if you follow the url_path regeneration feature of magento2 within category save you can see that the problem exists internaly
The error happens when you attempt to save the category and it regenerates neighbouring category url_path's along with each category's products. if two products have the same url_key they conflict within the category and causes your category save to fail :s
@WebKenth Thank you. I will give this a try. I know this module only shows you the problems. Does not fix. If I do find two products that have same url_key do I just manually delete url_key from Magento admin in url rewrites? Or do it from database? What would be the proper way to fix these conflicts?
@JonathanHelvey Yep or just change the product's url_key to something different
I handled it by updating the products url_key with a script that simply adds a number depending on the occurences :P
it can probably be done in multiple ways
@WebKenth, Sounds good thank you for the advice. I guess, I could also go with a paid module that will do it for me. But like the free module says. It can mess with SEO. The paid module is pretty cheap from Magento store. https://marketplace.magento.com/webpanda-catalog-url-rewrites.html.
May do this to save time. If your url_key script is an open source project and up on github I would not mind trying your script out as well? If you are willing to share. Otherwise I will do it manually or start writing something out. Thanks for all the help.
Most helpful comment
@toanthree I like your second option, works great for me, I only added the last line of the old function, that makes it work for real: