Commit accacadf authored by Alexander Pace's avatar Alexander Pace
Browse files

retry fix for auto_increment_insert

parent 1eb41a12
Pipeline #206270 passed with stages
in 15 minutes and 39 seconds
......@@ -2,7 +2,7 @@ from collections import OrderedDict
import logging
import re
from django.db import models, connection
from django.db import models, connection, IntegrityError
from django.db.models import Q, Max
from django.utils import six
from django.contrib.auth import get_user_model
......@@ -42,6 +42,7 @@ class AutoIncrementModel(models.Model):
"""
AUTO_FIELD = None
AUTO_CONSTRAINTS = None
max_retries = 5
class Meta:
abstract = True
......@@ -123,9 +124,21 @@ class AutoIncrementModel(models.Model):
else:
setattr(self, self.AUTO_FIELD, 1)
# Save object and check constraints:
self.full_clean()
super(AutoIncrementModel, self).save(*args, **kwargs)
# Save object and check constraints. Add ability to retry on the event
# of DB Integrity errors.
this_try = 0
while this_try < self.max_retries:
try:
self.full_clean()
super(AutoIncrementModel, self).save(*args, **kwargs)
break
except IntegrityError as e:
logger.warning("Database integrity error when saving {}. ",
"Incrementing and retrying.".format(self))
setattr(self, self.AUTO_FIELD,
getattr(self,self.AUTO_FIELD) + 1)
this_try += 1
def auto_increment_update(self, update_field_name, constraints=[],
allow_update_to_nonnull=False):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment