I created a django command to remove about 100.000 entries in our database, however we tested it and noticed that when the command is terminated or otherwise raises an exception, the delete()
is not applied.
For example the command is at 100 of the 100.000 and we terminate the command, all entries are still there and the 100 that should be gone are still there.
Does anybody know why this happens and if this behaviour can be disabled. I know this can be dangerous, but we need it.
I tried to look at the implementation, but I cannot figure it out.
Actually Django’s default behaviour is to run in autocommit
mode unless a transaction is active. In your case, a transaction is active.
Django implicitly uses transactions during operations that require multiple queries, especially delete()
and update()
queries to guarantee the integrity of these ORM operations.
So what you are experiencing is a transaction rollback due to the termination of the delete()
query.
While you cannot disable the implicit use of transactions for a specific query, you can disable Django’s use of transactions on a database server completely.
settings.py
DATABASES = {
"default": {
"AUTOCOMMIT": False,
"ENGINE": "django.db.backends.•••",
#•••Rest of code•••,
}
}
Note that this is not a silver bullet and this is a strange requirement. It is not portable and you will be at the mercy of the underlying database library and whatever its behaviour is unless you want to manipulate that too.
Everybody needs transaction rollback features when something goes wrong (for example; a network failure during a payment procedure). The reason why you don’t want it is what I call strange.