# Upsert doesn't work correctly.

**URL:** <https://forum.shopware.com/t/upsert-doesnt-work-correctly/72798>\
**Category:** Programming (EN)\
**Created:** [15. Februar 2021 um 16:35 UTC](https://forum.shopware.com/t/upsert-doesnt-work-correctly/72798 "2021-02-15T16:35:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DionantSolutions](https://avatars.discourse-cdn.com/v4/letter/d/7feea3/32.png) [@DionantSolutions](https://forum.shopware.com/u/DionantSolutions)\
**Post date:** [15. Februar 2021 um 16:35 UTC](https://forum.shopware.com/t/upsert-doesnt-work-correctly/72798/1 "2021-02-15T16:35:08Z")

</div>

When adding a product using upsert it creates the product correctly. But trying to ‚update‘ the product using the the same method doesn’t work, as I get an integrity constraint, saying the following;  
&nbsp;

```
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'BU-10121' for key 'product.uniq.product.product_number__version_id'

```

I add products using the following code;&nbsp;

```
$product = [
    'name' => (string) $product->name,
    'price' => [[
        'currencyId' => Defaults::CURRENCY,
        'gross' => 15,
        'net' => 10,
        'linked' => false
    ]],
    'manufacturer' => ['name' => 'Test'],
    'tax' => ['name' => '21%', 'taxRate' => 21],
    'stock' => (int) $product->qty,
    'id' => (string) $product->ean,
    'ean' => (string) $product->ean,
    'productNumber' => (string) $product->sku,
    'categories' => [
        ['Id' => 1, 'name' => (string) $product->type]
    ]
];

$this->productRepository->upsert([$product], Context::createDefaultContext());

```

How can i fix this?

---

<div class="post-metadata">

**Author:** ![RafaelKr](https://avatars.discourse-cdn.com/v4/letter/r/4af34b/32.png) [@RafaelKr](https://forum.shopware.com/u/RafaelKr)\
**Post date:** [19. Februar 2021 um 14:58 UTC](https://forum.shopware.com/t/upsert-doesnt-work-correctly/72798/2 "2021-02-19T14:58:07Z")

</div>

I just had the same problem because I accidentally assigned a new random Uuid for upsert.

```
 'id' =\> Uuid::randomHex(),

```

Do you maybe have products with a different ean but with the same productNumber (sku)?

&nbsp;

Also you should probably use a Uuid for your ID as it’s a BINARY(16) on database level. But I’m not sure about the side effects of using the ean as id.

---

<div class="post-metadata">

**Author:** ![buro71a](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/buro71a/32/14078_2.png) [@buro71a](https://forum.shopware.com/u/buro71a)\
**Post date:** [20. Februar 2023 um 19:59 UTC](https://forum.shopware.com/t/upsert-doesnt-work-correctly/72798/3 "2023-02-20T19:59:02Z")

</div>

You can use Uuid::fromStringToHex($ean) … it will hash the EAN and creates the same Uuid for the same EAN all the time. So your products will reliably get the same Uuid everytime.

So the upsert also works fine. You can simply specify the ‚id‘ =\> Uuid::fromStringToHex($ean) …

Rico
