Magento Forum

Page 3 of 3
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`irciycom_magento/weee_discoun
 
Branko56
Jr. Member
 
Total Posts:  14
Joined:  2008-12-11
 

I had the same issue and resolved it by ensuring all the necessary tables were using InnoDB. This mysql article is helpful in understanding the issue: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
CollinsHarper
Jr. Member
 
Avatar
Total Posts:  18
Joined:  2008-08-14
Vancouver, BC
 

Hello,

We also had the same problem with a clients site.

The wee_discount table stores a discount to be applied to each product.  If you look at the table, it includes a reference to the product, the website, the customer group and a value that will alter the catalog price displayed to the user.

In my case, the rule was

give a 10% discount to all customers in the ‘preferred customer group’ on all products”.

So, because this rule is to be applied to ‘ALL’ products, when you add a new product, it adds an entry into this table.  However, when you save the product, the forign key or product reference used to apply the discount is not the actual product key for the newly added product.  It is instead an auto increment.

So when we Iooked at the database, the last few catalog_product_entity.entity_id’s were:

1700
1701
1705

and the product I was inserting was to have an id of 1710.  Why?  because the client had deleted some products ; namely 1706-1709.

Meanwhile the weee_discount table’s last few entries were:

1700
1701
1705

and what does it try to do?  it tries to add a new record with an entity_id of 1706 which is the cause of the error.  the catalog_product_entity record with an id of 1706 has been deleted and the foreign key constraint requires there be a product there.

To fix the issue, I removed the rule and advised the client not to use that feature until we can apply a fix.

Hope that helps!

Sean

 
Magento Community Magento Community
Magento Community
Magento Community
 
MarkNewby
Member
 
Avatar
Total Posts:  32
Joined:  2008-10-06
MD of Camiloo Limited
 

For anyone having this issue with weee/discount, I think I might have a fix.

We’ve just had the issue on a clients site, and this fix solved it straight away [also it ensures it can’t happen again]
The fix we did was the following [this modifies the core - the change will be overwritten next time you upgrade magento]

In /app/code/core/Mage/Weee/Model/Mysql4/Tax.php at line 56 FIND

foreach ($productData as $product) {
$this->_getWriteAdapter()->insert($this->getTable(’weee/discount’), $product);
}

REPLACE WITH

foreach ($productData as $product) {

// bugfix by Mark Newby at Camiloo Limited - checks if product exists before continuing.
$theproduct = Mage::getModel(’catalog/product’)->loadByAttribute(’entity_id’,$product[’entity_id’]);
if(is_object($theproduct)){
unset($theproduct);
$this->_getWriteAdapter()->insert($this->getTable(’weee/discount’), $product);
}

}

This fix basically checks if the product exists by trying to load the product model - if this fails, it will do so in a much more graceful way.
I’m not aware of any noticeable impact on performance caused by doing this.

 
Magento Community Magento Community
Magento Community
Magento Community
 
rmwatson
Jr. Member
 
Total Posts:  4
Joined:  2009-03-17
 

I had the same error message when I tried to set a specific size within an item to ‘out of stock’.

To resolve this, I refreshed all cache, turned the item to ‘disabled’ (under general), then set it to ‘out of stock’ (inventory), then back to ‘enabled’ (general). Seemed to do the trick.

 
Magento Community Magento Community
Magento Community
Magento Community
 
guido7
Member
 
Total Posts:  40
Joined:  2009-07-04
 

try going to System > Cache Management in admin panel and refreshing your cache, and clicking on all the various refresh/rebuild buttons under ‘Catalog’.  Then try adding a new product.

Don’t know what all these things do and which are necessary, but doing this worked for me

good luck

 
Magento Community Magento Community
Magento Community
Magento Community
 
ravisoni6262
Jr. Member
 
Total Posts:  4
Joined:  2012-06-26
 

And the solution is here
http://www.magegurus.com/magento-solution-for-sql-integrity-constraint-violation/

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 3 of 3