search

Home  >  Q&A  >  body text

How to ensure the data is correct if two DMLs occur simultaneously in Django ORM?

My Django project uses the mysql and InnoDB engines. I have a table as shown below, the queue field default value is 0

class Task(models.Model):
    task_id = models.CharField(max_length=32, primary_key=True)
    queue = models.IntegerField(default=0)

I have two processes, one is to insert a record based on the maximum queue in the table.

max_queue = Task.objects.all().annotate(data=Max('queue')).values('data')
 queue_number = max_queue[0]['data'] + 1
 record = {'task_id':task_id, 'queue': queue_number}
 Task.objects.create(**record)

Another process is to decrement the value of queue by 1 for each record where queue

is not equal to 0.
query_list = Task.objects.filter(~Q(task_queue=0))
for query in query_list:
    Task.objects.filter(task_id=task_id).update(queue=query.queue - 1)

What I care about here is whether these two processes occur at the same time. For example, if the subsequent process wants to decrement each value. At the same time, the first process inserts a new value. In this case, some errors may occur.

what should I do? Anyone has a good idea, thanks in advance.

P粉041881924P粉041881924447 days ago561

reply all(1)I'll reply

  • P粉557957970
  • Cancelreply