Reputation: 603
Since our last release a range of what seems can only DB race conditions have been happening.We use python 3.10, django-rq, Django 4 and Postgres.
In one case:
x = Model.objects.create()
job.delay(x_id=x.id)
In the job we call Model.objects.get(id=x.id) and it raises 'matching query does not exist.' error. This is happening roughly 1 in 5 times from logs
In another case we create the object, add it to a list then act on the list and again it works most of the time but we are getting one off 'matching query does not exist.' errors.
I am completely stumped as to what I should do, I cant find many similar issues and can't recreate the issues in my test environment.
My best assumption is too many writes to the DB so the transactions aren't being committed by the time the code tries to get the objects again.
Any help/advice would be amazing
Have gone through the code step by step to ensure the logic is sound
Have tried recreating a 'busy' DB but have had no success there
Have tried broken data etc but it all gets caught and our logs don't suggest anything like this either
Upvotes: 0
Views: 188
Reputation: 1632
In this case a job to get
object should be scheduled after create
is committed to database.
This can be done using on_commit hook https://docs.djangoproject.com/en/4.2/topics/db/transactions/#performing-actions-after-commit
Example:
from django.db import transaction
@transaction.atomic
def your_method():
x = Model.objects.create()
transaction.on_commit(job.delay(x_id=x.id))
Upvotes: 1