Django update_or_create within a loop is duplicating field values, but just a couple fields

It's been a while but I have a somewhat puzzling issue. I am looping over some data being pulled from an api and performing an update_or_create call through django.

for product in response['products']:
    for variant in product['variants']:
        print(variant['product_id'])
        obj, created = Product.objects.update_or_create(
            sku=variant['sku'],
            defaults={
                'name': product['title'],
                'price': variant['price'],
                'sku': variant['sku'],
                'shopify_product_id': variant['product_id'],
                'shopify_variant_id': variant['id'],
                'weight_grams': variant['grams'],
            },
        )

During the loop you'll notice I am printing the value of the variant product id.

print(variant['product_id'])

In the console I see the print results are correct with different variant product ids.

7396903682207
7405275381919
7405275775135
7405273579679
7405278101663
7396921475231
9034125279391

But in the database both the variant['product_id'] and variant['id'] are all the same for every row. Yet the other details such as name and price are all correct and differ per row.

enter image description here

I can not for the life of me figure out why this is happening. I am using the sku as the unique identifier to filter on and it is set as unique in my model. No matter what I do or try, it is only those two fields that get duplicated.

Well, I am embarrassed to say I figured it out. I had the model fields set to integer and the integers that are used for the two IDs were larger than the integer field can handle. I switched them to big integer fields and now all is well!

Back to Top