Using Django in a MySQL database, I get the following error:
OperationalError: (1213, 'Deadlock found when trying to get lock; try restarting transaction')
The error occurs in the following code:
start_time = 1422086855 end_time = 1422088657 self.model.objects.filter( user=self.user, timestamp__gte=start_time, timestamp__lte=end_time).delete() for sample in samples: o = self.model(user=self.user) o.timestamp = sample.timestamp ... o.save()
I have several parallel processes working with the same database, and sometimes they can have the same job or match sample data. Therefore, I need to clear the database and then save new samples, since I do not want duplicates.
I run all this in a transactional block with transaction.commit_on_success() and quite often get an OperationalError exception. I would prefer that the transaction does not come to a standstill, but instead simply blocks and waits for another process to complete with its work.
From what I read, I have to order the locks correctly, but I'm not sure how to do this in Django.
What is the easiest way to ensure that I don't get this error while still making sure that I don't lose any data?
python django mysql deadlock
gurglet
source share