Magento 2 unique constraint violation found error when generating order

The “Unique constraint violation found” error during checkout in Magento 2 typically occurs when the system attempts to insert data into the database that violates a unique constraint defined for a specific column or set of columns. In simple words, some queries attempt to add duplicate entries to a database table, but the primary key prevents this from happening.

Duplicate order increment ID or quote ID is the most common cause of this error, but if this is not the case, then it’s too difficult to find which table has duplicate entries. Here, we show the method to get the exact table name that creates this issue.

How to retrieve the table name for an error involving a unique constraint violation

Follow the below steps to get the table name.
Step 1: Edit “AbstractDb.php” file from below location
Location: /vendor/magento/framework/Model/ResourceModel/Db

Step 2: There is a public function save() function. In this function, print the actual message using $e->getMessage()

We use the code below to get an exact error message.

file_put_contents(‘temp_debug.txt’, print_r($e->getMessage(), true), FILE_APPEND);

This code generates one file at the root of your server.

Note: Since the root of Adobe Commerce Cloud is read-only, the code mentioned above will not add a log file there; instead, you must debug this code locally for Adobe Commerce Cloud Project.

Below is a screenshot of how we did it.

I got that error message recently in our Adobe Commerce cloud project, and that was due to a custom module, so instead of finding duplicate entries, just print that actual error and save your time.

Check out our blog post, “How to Get Product Salable Quantity in Magento 2” to learn about Magento 2 salable qty.

Privacy Policy © 2024-2026 Dhairvi Solutions LLP. All Rights Reserved.