Django's select_for_update(skip_locked=True) not compatible with spanning
I was attempting to lock the oldest item in a queryset and bashed my head over why it was not working.
Every time I used this first code snippet the entire query set would be locked.
with transaction.atomic():
locked_entry = Entry.objects.select_for_update(skip_locked=True).filter(
event__date=distribution_date(),
status='pending',
entry_type__name='Premium'
).order_by('-created_at').first()
print(locked_entry)
sleep(4)
However, upon passing the exact FK instances the lock began to correctly only lock the oldest instance. I'm sure it has something to do with how the SQL is called but was hoping for an explanation if there are any Django experts out there :)
with transaction.atomic():
locked_entry = Entry.objects.select_for_update(skip_locked=True).filter(
event=Event.objects.get(date=distribution_date()),
status='pending',
entry_type=EntryType.objects.get(name='Premium')
).order_by('-created_at').first()
print(locked_entry)
sleep(4)