Ticket #16340: 16340_get_or_create_traceback.patch

File 16340_get_or_create_traceback.patch, 2.1 KB (added by Dougal Matthews, 13 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 6a6a829..cd474d7 100644
    a b The main QuerySet implementation. This provides the public API for the ORM.  
    44
    55import copy
    66from itertools import izip
     7import sys
    78
    89from django.db import connections, router, transaction, IntegrityError
    910from django.db.models.aggregates import Aggregate
    class QuerySet(object):  
    387388                return obj, True
    388389            except IntegrityError, e:
    389390                transaction.savepoint_rollback(sid, using=self.db)
     391                exc_info = sys.exc_info()
    390392                try:
    391393                    return self.get(**lookup), False
    392394                except self.model.DoesNotExist:
    393                     raise e
     395                    # re-raise the IntegrityError with the original traceback.
     396                    raise exc_info[1], None, exc_info[2]
    394397
    395398    def latest(self, field_name=None):
    396399        """
  • tests/modeltests/get_or_create/tests.py

    diff --git a/tests/modeltests/get_or_create/tests.py b/tests/modeltests/get_or_create/tests.py
    index 3323c88..0eac405 100644
    a b  
    11from datetime import date
     2import sys
     3import traceback
    24
    35from django.db import IntegrityError
    46from django.test import TestCase
    class GetOrCreateTests(TestCase):  
    5052            ManualPrimaryKeyTest.objects.get_or_create, id=1, data="Different"
    5153        )
    5254        self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, "Original")
     55
     56        # get_or_crete should raise IntegrityError's with the full traceback.
     57        # This is tested by checking that a know method call is in the traceback.
     58        try:
     59            ManualPrimaryKeyTest.objects.get_or_create(id=1, data="Different")
     60        except IntegrityError, e:
     61            formatted_traceback = traceback.format_exc()
     62            contains_insert_query = 'obj.save' in formatted_traceback
     63            self.assertTrue(contains_insert_query,
     64                "The traceback didn't contain 'obj.save'.")
     65 No newline at end of file
Back to Top