mirror of
https://github.com/django/django.git
synced 2025-09-26 12:09:19 +00:00
Fixed #20463 -- Made get_or_create more robust.
When an exception other than IntegrityError was raised, get_or_create could fail and leave the database connection in an unusable state. Thanks UloPe for the report.
This commit is contained in:
parent
adeec00979
commit
0e51d8eb66
2 changed files with 22 additions and 4 deletions
|
@ -9,7 +9,7 @@ import warnings
|
|||
|
||||
from django.conf import settings
|
||||
from django.core import exceptions
|
||||
from django.db import connections, router, transaction, IntegrityError
|
||||
from django.db import connections, router, transaction, DatabaseError
|
||||
from django.db.models.constants import LOOKUP_SEP
|
||||
from django.db.models.fields import AutoField
|
||||
from django.db.models.query_utils import (Q, select_related_descend,
|
||||
|
@ -382,13 +382,13 @@ class QuerySet(object):
|
|||
obj.save(force_insert=True, using=self.db)
|
||||
transaction.savepoint_commit(sid, using=self.db)
|
||||
return obj, True
|
||||
except IntegrityError:
|
||||
except DatabaseError:
|
||||
transaction.savepoint_rollback(sid, using=self.db)
|
||||
exc_info = sys.exc_info()
|
||||
try:
|
||||
return self.get(**lookup), False
|
||||
except self.model.DoesNotExist:
|
||||
# Re-raise the IntegrityError with its original traceback.
|
||||
# Re-raise the DatabaseError with its original traceback.
|
||||
six.reraise(*exc_info)
|
||||
|
||||
def _earliest_or_latest(self, field_name=None, direction="-"):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue