Reputation: 988
I am working with django on a large project.
I call a celery task
from inside the save
of a model that calls a method which calls another method in a loop. That is:
celery task --> function A()
A() --> for i in range(1,100): call function B()
Now B()
is wrapped with an atomic()
decorator and has a select_for_update
call inside it.
I still get TransactionManagementError('select_for_update cannot be used outside of a transaction.',)
I do not know why this is. I have tested with delaying the task by a few seconds so that the save
is committed by the time the task is called. Did not help.
My question is: Why am I getting a TransactionManagementError
when I am already inside an atomic block?
Upvotes: 4
Views: 1977
Reputation: 998
@ketanbhatt This might help
https://docs.djangoproject.com/en/1.9/ref/models/querysets/#select-for-update
Evaluating a queryset with select_for_update() in autocommit mode on backends which support SELECT ... FOR UPDATE is a TransactionManagementError error because the rows are not locked in that case. If allowed, this would facilitate data corruption and could easily be caused by calling code that expects to be run in a transaction outside of one.
https://docs.djangoproject.com/en/1.9/topics/db/transactions/#managing-autocommit
Django will refuse to turn autocommit off when an atomic() block is active, because that would break atomicity.
Upvotes: 2