Today something strange happened. I'm importing data from a payment gateway after request:
for signature in response.json:
Signature.objects.get_or_create(**signature)
json example:
[
{'id': 1, 'plan': 1, 'customer': 1},
{'id': 31, 'plan': 12, 'customer': 22}
{'id': 2, 'plan': 3, 'customer': 50},
{'id': 3111, 'plan': 12, 'customer': 22},
{'id': 222, 'plan': 12, 'customer': 22},
]
Yes, my client didn't follow an ID sequence registering signatures manually on payment service so, I'm importing and keeping the same pk.
This code works as expected and data is now synced with payment service (all objects imported).
Now the strange behavior:
I'm using Django Rest Framework and after a POST (check validated_data) in my API the following error raises at this line:
Signature.object.create(**self.validated_data)
duplicate key value violates unique constraint "plans_signature_pkey" DETAIL: Key (id)=(1) already exists.
validated data:
{
"plan": "3", # This is a foreign key to plan 3
"payer_only": False,
"schedule": "09:00",
"payment_method: "CREDIT_CARD"
}
There is no 'pk': 1 or 'id': 1 in validated data
Django is trying to create an object with an existing key?
Debugging code, I called the Subscription.create() line 31 times then:
duplicate key value violates unique constraint "plans_signature_pkey" DETAIL: Key (id)=(1) already exists.
....
duplicate key value violates unique constraint "plans_signature_pkey" DETAIL: Key (id)=(31) already exists.
On call 32 this works. So, am I missing something? This looks a weird behavior to me.