Hi,
I have inherited a legacy Item model that has a composite unique key consisting
of a Customer ID and a per-customer, incrementing Item ID. Assume I can't
change the model.
On inserts, the legacy code would let the database increment the Item ID to
avoid race conditions. Something like this:
INSERT INTO item_table (customer_id, item_id, name, ...) VALUES (123,
(SELECT MAX(item_id) FROM item_table WHERE customer_id =123) + 1, 'MyItem',
...);
Is there any way I can do the same using the Django ORM without opening up for
race conditions? I.e. something better than:
i = Item(customer_id=123, name='MyItem', ...)
i.item_id =
Item.objects.filter(customer_id=123).aggregate(Max('item_id'))['item_id__max']
+ 1
i.save()
Or do I just wrap that in a loop and catch IntegrityError if I don't want to
use raw SQL?
Thanks,
Erik
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/5361025E-2D7E-49D0-A704-47A165ECC217%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.