I have a system that generates quotes, then submits the quotes, which processes a payment. The system is pretty standard: nothing really complicated is going on. The quotes have a single product in them.
After the payment is processed, Magento saves the Order and the payment. However, sometimes we get an exception:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
sales_order_address
, CONSTRAINTSALES_ORDER_ADDRESS_PARENT_ID_SALES_ORDER_ENTITY_ID
FOREIGN KEY (parent_id
) REFERENCESsales_order
(entity_id
) ON DELETE CASCADE) in /var/www/html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228
This error suggests to me that it is trying to save the sales_order_address for a non-existent sales_order record, but why would that happen?
The payment was processed successfully on the backend but there is nothing recorded in the database to indicate that. Retrying the same quote again with the same input data usually results in success – the error appears to be transient.
How can I figure out what is causing the sales_order record to be deleted? How is this even possible? I tried searching the Magento code for a transaction rollback that swallows an exception, but didn’t find any. We are using a third-party Stripe payment module but the order-saving part happens after the payment was successfully placed. From what I can see whatever error is happening here is entirely in the core code. The error is happening on the latest (2.1.11) version.