Aller au contenu

Missing Transaction For Multi Write (ARCH2065)

Ce contenu n’est pas encore disponible dans votre langue.

Key python.orm.missing-transaction-for-multi-write · Severity warning · Category orm

Two or more dependent writes in the same method that are not wrapped in an explicit transaction/atomic block risk partial commits. Make the writes atomic.

def transfer(src, dst, amount):
src.balance -= amount
src.save() # a failure after this loses the money
dst.balance += amount
dst.save()
def transfer(src, dst, amount):
with transaction.atomic():
src.balance -= amount
src.save()
dst.balance += amount
dst.save()

Wrap dependent multi-write operations in transaction.atomic() so they commit all-or-nothing.