Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28479 closed Cleanup/optimization (fixed)

Document that transactions do not revert model state

Reported by: Todor Velichkov Owned by: Jonatas CD
Component: Documentation Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description

I know this may be obvious, but I think its worth mentioning in the documentation that when a transaction rollback happen your model/s state will not be reverted, thus the underlying model instance/s may end up in an inconsistent state if not manually reverted.

A simple toy example:

from django.db import IntegrityError, transaction
from django.contrib.auth.models import User

def update_user(pk, username):
    user = User.objects.get(pk=pk)
    try:
        with transaction.atomic():
            user.username = username
            user.is_active = False
            user.save()
            return user
    except IntegrityError:
        return user

since username is an unique field if an IntegrityError happen and the transaction got reverted, the user which we return will have an inconsistent state (if not manually reverted).

Change History (6)

comment:1 by Tim Graham, 7 years ago

Summary: Document that transactions do not revert model State.Document that transactions do not revert model state
Triage Stage: UnreviewedAccepted

The document to update is probably docs/topics/db/transactions.txt.

comment:2 by Jonatas CD, 7 years ago

Owner: changed from nobody to Jonatas CD
Status: newassigned

comment:3 by Jonatas CD, 7 years ago

Has patch: set

Here is the PR.

comment:4 by Tim Graham, 7 years ago

Patch needs improvement: set

comment:5 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In c9b22707:

Fixed #28479 -- Doc'd that transaction rollback doesn't revert model state.

comment:6 by Tim Graham <timograham@…>, 7 years ago

In 0221039:

[1.11.x] Fixed #28479 -- Doc'd that transaction rollback doesn't revert model state.

Backport of c9b22707b0703db6c6ddaebdd00e2cd33d182e40 from master

Note: See TracTickets for help on using tickets.
Back to Top