0

While saving product in backend,throws "Unable to save Stock Item".

I have tried after flushed the cache and reindexing but still exception throws.

I have reffered below link, but no luck

https://community.magento.com/t5/Magento-2-x-Technical-Issues/Cannot-add-products-in-Magento-2-1-11-quot-Unable-to-save-Stock/td-p/108369

In exception log throws

[2019-04-09 11:04:50] main.CRITICAL: Unable to save Stock Item {"exception":"[object] (Magento\Framework\Exception\CouldNotSaveException(code: 0): Unable to save Stock Item at /var/www/html/vega/vendor/magento/module-catalog-inventory/Model/Stock/StockItemRepository.php:183, Zend_Db_Statement_Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (vega.cataloginventory_stock_item, CONSTRAINT CATINV_STOCK_ITEM_STOCK_ID_CATINV_STOCK_STOCK_ID FOREIGN KEY (stock_id) REFERENCES cataloginventory_stock (stock_id) ON DELETE CASCADE), query was: INSERT INTO cataloginventory_stock_item (product_id, stock_id, qty, min_qty, use_config_min_qty, is_qty_decimal, backorders, use_config_backorders, min_sale_qty, use_config_min_sale_qty, max_sale_qty, use_config_max_sale_qty, is_in_stock, low_stock_date, notify_stock_qty, use_config_notify_stock_qty, manage_stock, use_config_manage_stock, stock_status_changed_auto, use_config_qty_increments, qty_increments, use_config_enable_qty_inc, enable_qty_increments, is_decimal_divided, website_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) at /var/www/html/vega/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:235, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (vega.cataloginventory_stock_item, CONSTRAINT CATINV_STOCK_ITEM_STOCK_ID_CATINV_STOCK_STOCK_ID FOREIGN KEY (stock_id) REFERENCES cataloginventory_stock (stock_id) ON DELETE CASCADE) at /var/www/html/vega/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228)"} []

10
  • You imported product via csv? Apr 9, 2019 at 10:33
  • yes, I imported via csv
    – Jaisa
    Apr 9, 2019 at 10:35
  • I have truncated the tables which is menioned in the above link, afer that i have tried to import products via csv. Now products are saved without quanity
    – Jaisa
    Apr 9, 2019 at 10:37
  • product have custom options? Apr 9, 2019 at 10:43
  • Have you tried reindex command? Apr 9, 2019 at 10:45

1 Answer 1

2

catalog_inventory_stock have filled default data when installing magento. (ref : module-catalog-inventory/Setup/Patch/Data/CreateDefaultStock.php) And the script truncate cataloginventory_stock. So you need to fill default data manually like as below.

"INSERT INTO cataloginventory_stock (stock_id, website_id, stock_name) VALUES (1, 0, 'Default');"

1
  • You are my hero! Thank you for that answer!
    – Alexey
    Oct 30, 2020 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.