Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20761 -- Fixed DatabaseError handling in get_or_create and upd…

…ate_or_create.
  • Loading branch information...
commit e716518ad29898fb25c820023aaf2fdd1c9eb4af 1 parent 33242fe
Loic Bistuer authored July 21, 2013 timgraham committed July 23, 2013

Showing 1 changed file with 13 additions and 12 deletions. Show diff stats Hide diff stats

  1. 25  django/db/models/query.py
25  django/db/models/query.py
@@ -8,7 +8,7 @@
8 8
 
9 9
 from django.conf import settings
10 10
 from django.core import exceptions
11  
-from django.db import connections, router, transaction, DatabaseError
  11
+from django.db import connections, router, transaction, DatabaseError, IntegrityError
12 12
 from django.db.models.constants import LOOKUP_SEP
13 13
 from django.db.models.fields import AutoField
14 14
 from django.db.models.query_utils import (Q, select_related_descend,
@@ -397,34 +397,35 @@ def update_or_create(self, defaults=None, **kwargs):
397 397
                 return obj, created
398 398
         for k, v in six.iteritems(filtered_defaults):
399 399
             setattr(obj, k, v)
  400
+
  401
+        sid = transaction.savepoint(using=self.db)
400 402
         try:
401  
-            sid = transaction.savepoint(using=self.db)
402 403
             obj.save(update_fields=filtered_defaults.keys(), using=self.db)
403 404
             transaction.savepoint_commit(sid, using=self.db)
404 405
             return obj, False
405 406
         except DatabaseError:
406 407
             transaction.savepoint_rollback(sid, using=self.db)
407  
-            six.reraise(sys.exc_info())
  408
+            six.reraise(*sys.exc_info())
408 409
 
409 410
     def _create_object_from_params(self, lookup, params):
410 411
         """
411 412
         Tries to create an object using passed params.
412 413
         Used by get_or_create and update_or_create
413 414
         """
  415
+        obj = self.model(**params)
  416
+        sid = transaction.savepoint(using=self.db)
414 417
         try:
415  
-            obj = self.model(**params)
416  
-            sid = transaction.savepoint(using=self.db)
417 418
             obj.save(force_insert=True, using=self.db)
418 419
             transaction.savepoint_commit(sid, using=self.db)
419 420
             return obj, True
420  
-        except DatabaseError:
  421
+        except DatabaseError as e:
421 422
             transaction.savepoint_rollback(sid, using=self.db)
422  
-            exc_info = sys.exc_info()
423  
-            try:
424  
-                return self.get(**lookup), False
425  
-            except self.model.DoesNotExist:
426  
-                # Re-raise the DatabaseError with its original traceback.
427  
-                six.reraise(*exc_info)
  423
+            if isinstance(e, IntegrityError):
  424
+                try:
  425
+                    return self.get(**lookup), False
  426
+                except self.model.DoesNotExist:
  427
+                    pass
  428
+            six.reraise(*sys.exc_info())
428 429
 
429 430
     def _extract_model_params(self, defaults, **kwargs):
430 431
         """

0 notes on commit e716518

Please sign in to comment.
Something went wrong with that request. Please try again.