Magento 2 : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails while saving a duplicate category

magento.stackexchange https://magento.stackexchange.com/questions/322107

Question

I have installed Duplicate categories Extension in the client site(Magento 2.3.2), while duplicating categories I'm facing this error,

{"0":"SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (my_db.catalog_url_rewrite_product_category, CONSTRAINT CAT_URL_REWRITE_PRD_CTGR_PRD_ID_CAT_PRD_ENTT_ENTT_ID FOREIGN KEY (product_id) REFERENCES catalog_product_entity (entity_), query was: INSERT INTO catalog_url_rewrite_product_category (url_rewrite_id,category_id,product_id`) VALUES (?, ?, ?),

{"0":"SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`my_db`.`catalog_url_rewrite_product_category`, CONSTRAINT `CAT_URL_REWRITE_PRD_CTGR_PRD_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_), query was: INSERT  INTO `catalog_url_rewrite_product_category` (`url_rewrite_id`,`category_id`,`product_id`) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?)","1":"<pre>#1 Magento\\Framework\\DB\\Statement\\Pdo\\Mysql->_execute() called at [vendor\/magento\/zendframework1\/library\/Zend\/Db\/Statement.php:303]\n#2 Zend_Db_Statement->execute() called at [vendor\/magento\/zendframework1\/library\/Zend\/Db\/Adapter\/Abstract.php:480]\n#3 Zend_Db_Adapter_Abstract->query() called at [vendor\/magento\/zendframework1\/library\/Zend\/Db\/Adapter\/Pdo\/Abstract.php:238]\n#4 Zend_Db_Adapter_Pdo_Abstract->query() called at [vendor\/magento\/framework\/DB\/Adapter\/Pdo\/Mysql.php:542]\n#5 Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql->_query() called at [vendor\/magento\/framework\/DB\/Adapter\/Pdo\/Mysql.php:617]\n#6 Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql->query() called at [vendor\/magento\/framework\/DB\/Adapter\/Pdo\/Mysql.php:2067]\n#7 Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql->insertArray() called at [vendor\/magento\/framework\/DB\/Adapter\/Pdo\/Mysql.php:2024]\n#8 Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql->insertMultiple() called at [vendor\/magento\/module-catalog-url-rewrite\/Model\/ResourceModel\/Category\/Product.php:58]\n#9 Magento\\CatalogUrlRewrite\\Model\\ResourceModel\\Category\\Product->saveMultiple() called at [vendor\/magento\/module-catalog-url-rewrite\/Model\/Category\/Plugin\/Storage.php:57]\n#10 Magento\\CatalogUrlRewrite\\Model\\Category\\Plugin\\Storage->afterReplace() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:146]\n#11 Scriptlodge\\UrlRewrite\\Model\\Storage\\DbStorage\\Interceptor->Magento\\Framework\\Interception\\{closure}() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:153]\n#12 Scriptlodge\\UrlRewrite\\Model\\Storage\\DbStorage\\Interceptor->___callPlugins() called at [generated\/code\/Scriptlodge\/UrlRewrite\/Model\/Storage\/DbStorage\/Interceptor.php:39]\n#13 Scriptlodge\\UrlRewrite\\Model\\Storage\\DbStorage\\Interceptor->replace() called at [vendor\/magento\/module-catalog-url-rewrite\/Model\/UrlRewriteBunchReplacer.php:35]\n#14 Magento\\CatalogUrlRewrite\\Model\\UrlRewriteBunchReplacer->doBunchReplace() called at [vendor\/magento\/module-catalog-url-rewrite\/Observer\/CategoryProcessUrlRewriteSavingObserver.php:114]\n#15 Magento\\CatalogUrlRewrite\\Observer\\CategoryProcessUrlRewriteSavingObserver->execute() called at [vendor\/magento\/framework\/Event\/Invoker\/InvokerDefault.php:88]\n#16 Magento\\Framework\\Event\\Invoker\\InvokerDefault->_callObserverMethod() called at [vendor\/magento\/framework\/Event\/Invoker\/InvokerDefault.php:74]\n#17 Magento\\Framework\\Event\\Invoker\\InvokerDefault->dispatch() called at [vendor\/magento\/framework\/Event\/Manager.php:66]\n#18 Magento\\Framework\\Event\\Manager->dispatch() called at [generated\/code\/Magento\/Framework\/Event\/Manager\/Proxy.php:95]\n#19 Magento\\Framework\\Event\\Manager\\Proxy->dispatch() called at [vendor\/magento\/framework\/Model\/AbstractModel.php:829]\n#20 Magento\\Framework\\Model\\AbstractModel->afterSave() called at [vendor\/magento\/module-catalog\/Model\/Category.php:1171]\n#21 Magento\\Catalog\\Model\\Category->afterSave() called at [vendor\/magento\/framework\/EntityManager\/Observer\/AfterEntitySave.php:34]\n#22 Magento\\Framework\\EntityManager\\Observer\\AfterEntitySave->execute() called at [vendor\/magento\/framework\/Event\/Invoker\/InvokerDefault.php:88]\n#23 Magento\\Framework\\Event\\Invoker\\InvokerDefault->_callObserverMethod() called at [vendor\/magento\/framework\/Event\/Invoker\/InvokerDefault.php:74]\n#24 Magento\\Framework\\Event\\Invoker\\InvokerDefault->dispatch() called at [vendor\/magento\/framework\/Event\/Manager.php:66]\n#25 Magento\\Framework\\Event\\Manager->dispatch() called at [generated\/code\/Magento\/Framework\/Event\/Manager\/Proxy.php:95]\n#26 Magento\\Framework\\Event\\Manager\\Proxy->dispatch() called at [vendor\/magento\/framework\/EntityManager\/EventManager.php:52]\n#27 Magento\\Framework\\EntityManager\\EventManager->dispatchEntityEvent() called at [vendor\/magento\/framework\/EntityManager\/Operation\/Create.php:123]\n#28 Magento\\Framework\\EntityManager\\Operation\\Create->execute() called at [vendor\/magento\/framework\/EntityManager\/EntityManager.php:96]\n#29 Magento\\Framework\\EntityManager\\EntityManager->save() called at [vendor\/magento\/module-catalog\/Model\/ResourceModel\/Category.php:1106]\n#30 Magento\\Catalog\\Model\\ResourceModel\\Category->save() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:58]\n#31 Magento\\Catalog\\Model\\ResourceModel\\Category\\Interceptor->___callParent() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:138]\n#32 Magento\\Catalog\\Model\\ResourceModel\\Category\\Interceptor->Magento\\Framework\\Interception\\{closure}() called at [vendor\/magento\/module-catalog-search\/Model\/Indexer\/Fulltext\/Plugin\/Category.php:44]\n#33 Magento\\CatalogSearch\\Model\\Indexer\\Fulltext\\Plugin\\Category->addCommitCallback() called at [vendor\/magento\/module-catalog-search\/Model\/Indexer\/Fulltext\/Plugin\/Category.php:28]\n#34 Magento\\CatalogSearch\\Model\\Indexer\\Fulltext\\Plugin\\Category->aroundSave() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:135]\n#35 Magento\\Catalog\\Model\\ResourceModel\\Category\\Interceptor->Magento\\Framework\\Interception\\{closure}() called at [vendor\/magento\/framework\/App\/Cache\/FlushCacheByTags.php:68]\n#36 Magento\\Framework\\App\\Cache\\FlushCacheByTags->aroundSave() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:135]\n#37 Magento\\Catalog\\Model\\ResourceModel\\Category\\Interceptor->Magento\\Framework\\Interception\\{closure}() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:153]\n#38 Magento\\Catalog\\Model\\ResourceModel\\Category\\Interceptor->___callPlugins() called at [generated\/code\/Magento\/Catalog\/Model\/ResourceModel\/Category\/Interceptor.php:52]\n#39 Magento\\Catalog\\Model\\ResourceModel\\Category\\Interceptor->save() called at [vendor\/magento\/framework\/Model\/AbstractModel.php:655]\n#40 Magento\\Framework\\Model\\AbstractModel->save() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:58]\n#41 Magento\\Catalog\\Model\\Category\\Interceptor->___callParent() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:138]\n#42 Magento\\Catalog\\Model\\Category\\Interceptor->Magento\\Framework\\Interception\\{closure}() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:153]\n#43 Magento\\Catalog\\Model\\Category\\Interceptor->___callPlugins() called at [generated\/code\/Magento\/Catalog\/Model\/Category\/Interceptor.php:26]\n#44 Magento\\Catalog\\Model\\Category\\Interceptor->save() called at [app\/code\/MyVendor\/DuplicateCategories\/Controller\/Adminhtml\/Category\/Save.php:141]\n#45 MyVendor\\DuplicateCategories\\Controller\\Adminhtml\\Category\\Save->duplicate() called at [app\/code\/MyVendor\/DuplicateCategories\/Controller\/Adminhtml\/Category\/Save.php:63]\n#46 MyVendor\\DuplicateCategories\\Controller\\Adminhtml\\Category\\Save->execute() called at [vendor\/magento\/framework\/App\/Action\/Action.php:108]\n#47 Magento\\Framework\\App\\Action\\Action->dispatch() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:58]\n#48 MyVendor\\DuplicateCategories\\Controller\\Adminhtml\\Category\\Save\\Interceptor->___callParent() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:138]\n#49 MyVendor\\DuplicateCategories\\Controller\\Adminhtml\\Category\\Save\\Interceptor->Magento\\Framework\\Interception\\{closure}() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:153]\n#50 MyVendor\\DuplicateCategories\\Controller\\Adminhtml\\Category\\Save\\Interceptor->___callPlugins() called at [generated\/code\/MyVendor\/DuplicateCategories\/Controller\/Adminhtml\/Category\/Save\/Interceptor.php:26]\n#51 MyVendor\\DuplicateCategories\\Controller\\Adminhtml\\Category\\Save\\Interceptor->dispatch() called at [vendor\/magento\/framework\/App\/FrontController.php:159]\n#52 Magento\\Framework\\App\\FrontController->processRequest() called at [vendor\/magento\/framework\/App\/FrontController.php:99]\n#53 Magento\\Framework\\App\\FrontController->dispatch() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:58]\n#54 Magento\\Framework\\App\\FrontController\\Interceptor->___callParent() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:138]\n#55 Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}() called at [vendor\/magento\/framework\/Interception\/Interceptor.php:153]\n#56 Magento\\Framework\\App\\FrontController\\Interceptor->___callPlugins() called at [generated\/code\/Magento\/Framework\/App\/FrontController\/Interceptor.php:26]\n#57 Magento\\Framework\\App\\FrontController\\Interceptor->dispatch() called at [vendor\/magento\/framework\/App\/Http.php:137]\n#58 Magento\\Framework\\App\\Http->launch() called at [generated\/code\/Magento\/Framework\/App\/Http\/Interceptor.php:24]\n#59 Magento\\Framework\\App\\Http\\Interceptor->launch() called at [vendor\/magento\/framework\/App\/Bootstrap.php:261]\n#60 Magento\\Framework\\App\\Bootstrap->run() called at [index.php:39]\n<\/pre>","url":"\/trsadmin\/duplicatecategories\/category\/save\/key\/e136aa1e297bb63596d0a11d8a079ef27c663ca61e6b959948bfe185175a0bd6\/","script_name":"\/index.php"}

EDIT

Error is showing at this line in $copy->save()

$copy = $this->categoryFactory->create();

$copy->setIsActive(true);
$copy->setName($category->getName());
$copy->setParentId($parentCategory->getId());   
$copy->setPath($parentCategory->getPath());
$copy->setData('description',$sub_description);
$copy->setData('meta_description',$sub_meta_description);
$copy->setData('meta_keywords',$sub_meta_keywords);    

$newUrl = $this->getNewUrl($category, $parentCategory,$count);
$copy->setUrlKey($newUrl);

if($copy_products == 1){   

    $copy->setPostedProducts($category->getProductsPosition());
}
$copy->save();

I have also tried saving with store id and repository it doesn't work for the category that I want to duplicate, code is working for other categories. I think there is a non-existential category id or product id i don't understand how do I find that in the database.

Client has done Migration on the site before installing the module.

Does anyone has any idea? why this error is showing and how do I resolve it.

Was it helpful?

Solution

Finally, I resolved this issue with the help of this Reference link

Ran below query to find and delete duplicates,

Delete from url_rewrite where entity_type='product' and entity_id NOT IN (Select entity_id from catalog_product_entity);

After deleting I ran upgrade-deploy and indexer commands and it's working for me.

Hope it Helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top