I am getting the following error while trying to add product to cart in Magento 2.2.3.
[2018-05-02 09:52:43] report.CRITICAL: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
magento2
.quote_item
, CONSTRAINTQUOTE_ITEM_QUOTE_ID_QUOTE_ENTITY_ID
FOREIGN KEY (quote_id
) REFERENCESquote
(entity_id
) ON DELETE CASCADE), query was: INSERT INTOquote_item
(product_id
,store_id
,is_virtual
,sku
,name
,is_qty_decimal
,weight
,qty
,product_type
,base_cost
,subscription_frequency
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) {"exception":"[object] (Zend_Db_Statement_Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (magento2
.quote_item
, CONSTRAINTQUOTE_ITEM_QUOTE_ID_QUOTE_ENTITY_ID
FOREIGN KEY (quote_id
) REFERENCESquote
(entity_id
) ON DELETE CASCADE), query was: INSERT INTOquote_item
(product_id
,store_id
,is_virtual
,sku
,name
,is_qty_decimal
,weight
,qty
,product_type
,base_cost
,subscription_frequency
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) at C:\xampp\htdocs\mage2\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 (magento2
.quote_item
, CONSTRAINTQUOTE_ITEM_QUOTE_ID_QUOTE_ENTITY_ID
FOREIGN KEY (quote_id
) REFERENCESquote
(entity_id
) ON DELETE CASCADE) at C:\xampp\htdocs\mage2\vendor\magento\zendframework1\library\Zend\Db\Statement\Pdo.php:228)"} []
I have added a custom column in quote_item table. Not sure if this is the issue. Kindly help resolve it. TIA
EDIT:
I'm updating my custom using event - checkout_cart_product_add_after . Below is my observer update code.
<?php
namespace Addon\Recur\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
class CheckoutCartProductAddAfterObserver implements ObserverInterface
{
/**
* @var \Magento\Framework\View\LayoutInterface
*/
protected $_layout;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
protected $_request;
/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\View\LayoutInterface $layout
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\View\LayoutInterface $layout,
\Magento\Framework\App\RequestInterface $request
)
{
$this->_layout = $layout;
$this->_storeManager = $storeManager;
$this->_request = $request;
}
/**
* Add order information into GA block to render on checkout success pages
*
* @param EventObserver $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer) {
/* @var \Magento\Quote\Model\Quote\Item $item */
$item = $observer->getQuoteItem();
$post = $this->_request->getParam('frequency');
$item->setSubscriptionFrequency($post);
$item->save();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info($post);
}
}