Integrity constraint violation while importing products in magento

1.3k views Asked by At

I am trying to import products in magento through a csv file.

I got this error while importing the file.

please suggest me how resolve this error

I got the following error

    SQLSTATE[23000]: **Integrity constraint violation: 1452** Cannot add or update a child row: a foreign key constraint fails
(`cataloginventory_stock_item`, CONSTRAINT
`FK_CATINV_STOCK_ITEM_PRD_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY 
(`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE
 CA)
2

There are 2 answers

0
Emipro Technologies Pvt. Ltd. On BEST ANSWER

It seems that you CSV data file has some row(s) containing product id that doesn't exist in catalog_product_entity. Therefore it throws such exception. I think you need to verify product id value of imported CSV file.

0
evensis On

Had something similar, found this thread so thought i'd post a resolution. If using the cataloginventory/stock_item model, note that you have to use loadByProduct() and not load():

The below will throw this exact error you have described, as you're trying to load a product, but its expecting a primary key for that table.

Mage::getModel('cataloginventory/stock_item')->load($child['parent_id']);

Instead use:

Mage::getModel('cataloginventory/stock_item')->loadByProduct($child['parent_id']);

Hope this helps someone!