Hey Erik,

What Django version are you on?

On Tuesday, May 24, 2016 at 1:28:57 AM UTC+5:30, Erik Cederstrand wrote:
>
> 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/cb1f1135-416b-44b0-8b2c-c4415ff47e69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to