Skip to content

Handle transaction rollbacks #148

@lukebryant

Description

@lukebryant

Currently, if we do:

tm.field = 'new field value'
tm.get_dirty_fields() #returns {'field': 'original field value'}
try:
    with transaction.atomic(): 
        tm.save()
        raise Exception #force a rollback
except Exception:
     pass
tm.get_dirty_fields() #returns {}

This behaviour is problematic, because field in-memory and database values are still different.

It seems like this will be tricky to solve.

We could have a property uncommitted_dirty_fields, which we could clear in an on_commit() signal, but I can't think of a way of setting dirty_fields = uncommitted_dirty_fields in the case of a rollback, since there is no on_rollback() signal.

edit: If we had some way of uniquely identifying the current transaction (I can't see one in current django api), we might be able set dirty_fields = uncommitted_dirty_fields inside get_dirty_fields() if transaction_at_time_of_last_save != current_transaction. Then there'd be some additional complexity from rollbacks inside nested transactions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions