Wednesday, March 27, 2013

Magento : Solution for SQL Integrity constraint violation

I was working on client project with a special requirement which says product prices should update daily but after some time i have started getting this error in magento admin while trying to add new products
[php]
QLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`irciycom_magento/weee_discount`, CONSTRAINT `FK_CATALOG_PRODUCT_ENTITY_WEEE_DISCOUNT_PRODUCT_ENTITY` FOREIGN KEY (`entity_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASC).
[/php]

The solution to problem is open the file Url.php located at app/code/core/Mage/Catalog/Model/Resource and edit the function public function getProductModel() located at around line 113
[php]
public function getProductModel()
{
return Mage::getSingleton('catalog/product');
}
[/php]
to
[php]
public function getProductModel()
{
return Mage::getModel('catalog/product');
}
[/php]

And that's it, solved the error gone.

Alternatively if you don't want to edit the core files just follow this steps
1. Create these folders under the app/code/local , Mage/Catalog/Model/Resource
2. Copy the file Url.php from core to Resource folder and do the changes to Url.php and save it
3. You are done won battle with magento.

You could be in thinking what does getSingleton and getModel do. so you need to read this post.
Enjoy :-)

No comments:

Post a Comment