I have Django 1.11 app with PostgreSQL. Take a look at code below.
Is it possible to have race condition there? I am afraid I could get race condition on diff=-account.hours. Does transaction.atomic save from race condition?
from django.db import transaction def write_off(account_ids): accounts = Account.objects.filter(id__in=account_ids) for account in accounts: with transaction.atomic(): MyLog.objects.create( hours=0, operation_type=LOG_OPERATION_WRITE_OFF, diff=-account.hours, ) Account.objects.filter(pk=account.pk).update(hours=0)
accountwill be aQuerySetand not a singleAccountinstance. Therefor it will not have.hoursand.pk.diff=-account.hours. I takeaccountinstance, then saveaccount.hourstoMyLog, and then setaccount.hours=0. Could the value ofaccount.hoursbe changed after I createMyLogwith outdated value?