In django transactions, what happens if an error is raised in on_commit?

Situation

I have a postgres database, and I'm using django transaction's on_commit hook to do something (in this case move a file but it doesn't really matter what for the purposes of the question) once the save of a model has been committed to the database.

from django.db import models, transaction

def my_oncommit_callback():
    # Do something on commit
    raise ValueError("what if I raise an error")

class MyModel(models.Model):
    def save(self, *args, **kwwargs):
        transaction.on_commit(my_oncommit_callback)
        super().save(*args, **kwargs)

The Question

If my_oncommit_callback raises an error, is the transaction rolled back, or will I get a broken state because my on_commit function hasn't worked as expected?

From your own link in the question, Django's documentation itself states

Callbacks are called after the open transaction is successfully committed

So the failure of the callback won't have an effect on the original transaction, since it's already been committed.

Вернуться на верх