#7402: get_or_create can cause a transaction to fail silently
-------------------------------------------+--------------------------------
Reporter: [EMAIL PROTECTED] | Owner: nobody
Status: new | Milestone:
Component: Database wrapper | Version: SVN
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
-------------------------------------------+--------------------------------
Changes (by nullie):
* needs_better_patch: => 0
* has_patch: 0 => 1
* needs_tests: => 0
* needs_docs: => 0
Comment:
Following patch is a hack, but not more that concurrency problem solution
is. It still can get messy with managed transactions (but we shouldn't
have concurrency problems there, should we?)
Anyway, it still better to have somewhat working solution.
{{{
Index: django/db/models/query.py
===================================================================
--- django/db/models/query.py (revision 7932)
+++ django/db/models/query.py (working copy)
@@ -319,6 +319,7 @@
Returns a tuple of (object, created), where created is a boolean
specifying whether an object was created.
"""
+
assert kwargs, \
'get_or_create() must be passed at least one keyword
argument'
defaults = kwargs.pop('defaults', {})
@@ -332,7 +333,16 @@
obj.save()
return obj, True
except IntegrityError, e:
- return self.get(**kwargs), False
+ # Try to get object, maybe this was just concurrency
error.
+ try:
+ transaction.rollback_unless_managed()
+ obj = self.get(**kwargs)
+ except self.model.DoesNotExist:
+ # No object found, raise error
+ raise e
+
+ return obj, False
+
def latest(self, field_name=None):
"""
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/7402#comment:1>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---