Magento DB Error: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

StackOverflow https://stackoverflow.com/questions/22637390

Question

right now i am writing a custom importscript for Magento products, categories and attribute sets. I know it's bad practice to use bare MySQL and PHP, but that's the way it is at the moment.

While creating a product with my custom functions this error appears on the very first product:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: 
Integrity constraint violation: 1452 Cannot add or update a child row: 
a foreign key
constraint fails (`shopname`.`cataloginventory_stock_status`, CONSTRAINT 
`FK_CATINV_STOCK_STS_STOCK_ID_CATINV_STOCK_STOCK_ID` FOREIGN KEY (`stock_id`) 
REFERENCES `cataloginventory_stock` (`stock_id`) ON DE)' in 
/www/lib/Zend/Db/Statement/Pdo.php:228
Stack trace: #0 /www/lib/Zend/Db/Statement/Pdo.php(228): 
PDOStatement->execute(Array) #1 
/www/lib/Varien/Db/Statement/Pdo/Mysql.php(110):
Zend_Db_Statement_Pdo->_execute(Array) #2 
/www/app/code/core/Zend/Db/Statement.php(291):
Varien_Db_Statement_Pdo_Mysql->_execute(Array) #3 
/www/lib/Zend/Db/Adapter/Abstract.php(479): 
Zend_Db_Statement->execute(Array) #4 
/www/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): 
Zend_Db_Adapter_Abstract->q in 
/www/lib/Zend/Db/Statement/Pdo.php on line 234

I poked through stack overflow, but could not find anything suitable for the 'cataloginventory_stock_status'-error.

Could this be some leftover data from previous deletion of datasets?

Could you guys clarify what this specific error is about?

Was it helpful?

Solution

Some code might help, but I think you are trying to insert a row in the table cataloginventory_stock_status and the stock_id field is not filled in or is a value that is not present in the table cataloginventory_stock on the column stock_id.

By default Magento only has 1 row in the cataloginventory_stock table with id 1 (I don't even know what it means).

Set that field to 1 and maybe it will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top