#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                     |  
-------------------------------------------+--------------------------------
Comment (by nullie):

 Here is better patch, it fail correctly in manual transaction mode.

 {{{
 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,20 @@
                  obj.save()
                  return obj, True
              except IntegrityError, e:
 -                return self.get(**kwargs), False
 +                # If transactions are managed manually, we must fail.
 +                if transaction.is_managed():
 +                    raise e
 +
 +                # 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, re-raise error.
 +                     raise e
 +
 +                return obj, False
 +

      def latest(self, field_name=None):
          """
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/7402#comment:2>
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to