1

We are on Magento 2.2.8 and I was debugging the following issue for hours:

When sending a product-update via the REST API, we were getting

Unique constraint violation found

for a request as simple as

PUT https://www.example.de/rest/german/V1/products/B1234

Request body:

{"product": {"sku": "B1234","customAttributes": [{"attributeCode": "meta_keyword","value": "Bälle"}]}}}

The big problem is, that I cannot reproduce it using the Production Database Dump locally.

It seems to be strongly related to products which have a tier price in the database (which I am not updating in the REST call).

Luckily I was able to reproduce it somehow on a staging instance on the same machine as production and trace there error down to

vendor/magento/module- catalog/Model/Product/Attribute/Backend/TierPrice/UpdateHandler.php

There is the following code

        // prepare original data to compare
        $origPrices = $entity->getOrigData($attribute->getName());
        $old = $this->prepareOldTierPriceToCompare($origPrices);
        // prepare data for save
        $new = $this->prepareNewDataForSave($priceRows, $isGlobal);

        $delete = array_diff_key($old, $new);
        $insert = array_diff_key($new, $old);
        $update = array_intersect_key($new, $old);

Using var_dump-driven development techniques I was able to see, that $old is empty even there IS a database row for the tier price.

Actually the whole getOrigData() is empty.

That causes having $insert a value which IS already in the database

So MySQL (MariadB) throws an error at the following insertValues call, which tries to insert the already existing value.

        $isAttributeChanged = $this->deleteValues($productId, $delete);
        $isAttributeChanged |= $this->insertValues($productId, $insert);
        $isAttributeChanged |= $this->updateValues($update, $old);

This is the error:

 "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '19211-1-0-10.0000-0' for key 'UNQ_E8AB433B9ACB00343ABB312AD2FAB087', query was: INSERT INTO `catalog_product_entity_tier_price` (`entity_id`, `all_groups`, `customer_group_id`, `qty`, `value`, `percentage_value`, `website_id`) VALUES (?, ?, ?, ?, ?, ?, ?)"

Now the interesting part: If I clean the Magento cache, the problem disappears. For a while. But after another while seems to / might happen again.

Did anybody see such behavior before? Should we just refactor the core to not be so dependent on getOrigData?

EDIT The bug can be reproduced after some time again. Clearing the cache then fixes it. For now I stored the redis cache in a backup and will try to reproduce the issue locally by loading that cache.

Your Answer

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

Browse other questions tagged or ask your own question.