[Django]-Django – Rollback is not happening when exception raised

5👍

This is the correct. Behaviour django will rollback a transaction, if an exception occurs but that exeption has to be a DatabaseError or one of it’s subclasses (most notably IntegrityError)

The ObjectDoesNotExist is not a subclass of DatabaseError and as such there is no reason for this transaction to be rolled back.

last but not least. Don’t catch Exception always catch the specific exception that you are looking out for.

👤e4c5

0👍

If you have multiple databases configured you might want to specify the database to use with the using parameter. By default it uses the "default" database

with transaction.atomic(using="custom-db"):
    # your logic that raises error

https://docs.djangoproject.com/en/1.8/topics/db/transactions/#django.db.transaction.atomic

Leave a comment