I am using this code to add a stock item:
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->setData(array(
'use_config_manage_stock' => 0,
'manage_stock'=>1,
'min_sale_qty'=>1,
'max_sale_qty'=>2,
'is_in_stock' => 1,
'qty' => 999
)
);
$stockItem->save();
I am getting this error:
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 (
usr_web29785976_1
.cataloginventory_stock_item
, CONSTRAINTFK_CATINV_STOCK_ITEM_PRD_ID_CAT_PRD_ENTT_ENTT_ID
FOREIGN KEY (product_id
) REFERENCEScatalog_product_entity
(entity_id
) ON)' in /var/www/web29785976/html/magento/lib/Zend/Db/Statement/Pdo.php:228 Stack trace: #0 /var/www/web29785976/html/magento/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array) #1 /var/www/web29785976/html/magento/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array) #2 /var/www/web29785976/html/magento/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array) #3 /var/www/web29785976/html/magento/lib/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array) #4 /var/www/web29785976/html/magento/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT in /var/www/web29785976/html/magento/lib/Zend/Db/Statement/Pdo.php on line 235