Permalink
Browse files

Fixed #6641 -- If we lose a race when creating a new object in get_o…

…r_create,

re-get the result from the database and return that. Thanks, Jeff Guinness and
Timothée Peignier.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7289 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
malcolmt committed Mar 18, 2008
1 parent e534228 commit 1b331f6c1eadc55e6efe014ec5f5b91df83961ff
Showing with 9 additions and 6 deletions.
  1. +9 −6 django/db/models/query.py
View
@@ -1,5 +1,5 @@
from django.conf import settings
-from django.db import connection, transaction
+from django.db import connection, transaction, IntegrityError
from django.db.models.fields import DateField, FieldDoesNotExist
from django.db.models import signals, loading
from django.dispatch import dispatcher
@@ -285,11 +285,14 @@ def get_or_create(self, **kwargs):
try:
return self.get(**kwargs), False
except self.model.DoesNotExist:
- params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
- params.update(defaults)
- obj = self.model(**params)
- obj.save()
- return obj, True
+ try:
+ params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
+ params.update(defaults)
+ obj = self.model(**params)
+ obj.save()
+ return obj, True
+ except IntegrityError, e:
+ return self.get(**kwargs), False
def latest(self, field_name=None):
"""

0 comments on commit 1b331f6

Please sign in to comment.