Author: PaulM
Date: 2011-09-21 21:30:20 -0700 (Wed, 21 Sep 2011)
New Revision: 16873

Modified:
   django/trunk/django/core/management/commands/loaddata.py
   django/trunk/tests/modeltests/fixtures/tests.py
Log:
Fixed #16026 -- loaddata now identifies which object triggered an error.


Modified: django/trunk/django/core/management/commands/loaddata.py
===================================================================
--- django/trunk/django/core/management/commands/loaddata.py    2011-09-22 
04:16:21 UTC (rev 16872)
+++ django/trunk/django/core/management/commands/loaddata.py    2011-09-22 
04:30:20 UTC (rev 16873)
@@ -12,7 +12,8 @@
 from django.core import serializers
 from django.core.management.base import BaseCommand
 from django.core.management.color import no_style
-from django.db import connections, router, transaction, DEFAULT_DB_ALIAS
+from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS,
+      IntegrityError, DatabaseError)
 from django.db.models import get_apps
 from django.utils.itercompat import product
 
@@ -177,7 +178,16 @@
                                         if router.allow_syncdb(using, 
obj.object.__class__):
                                             loaded_objects_in_fixture += 1
                                             models.add(obj.object.__class__)
-                                            obj.save(using=using)
+                                            try:
+                                                obj.save(using=using)
+                                            except (DatabaseError, 
IntegrityError), e:
+                                                msg = "Could not load 
%(app_label)s.%(object_name)s(pk=%(pk)s): %(error_msg)s" % {
+                                                        'app_label': 
obj.object._meta.app_label,
+                                                        'object_name': 
obj.object._meta.object_name,
+                                                        'pk': obj.object.pk,
+                                                        'error_msg': e
+                                                    }
+                                                raise e.__class__, 
e.__class__(msg), sys.exc_info()[2]
 
                                 # Since we disabled constraint checks, we must 
manually check for
                                 # any invalid keys that might have been added

Modified: django/trunk/tests/modeltests/fixtures/tests.py
===================================================================
--- django/trunk/tests/modeltests/fixtures/tests.py     2011-09-22 04:16:21 UTC 
(rev 16872)
+++ django/trunk/tests/modeltests/fixtures/tests.py     2011-09-22 04:30:20 UTC 
(rev 16873)
@@ -252,6 +252,17 @@
             '<Article: Python program becomes self aware>'
         ])
 
+    def test_loaddata_error_message(self):
+        """
+        Verifies that loading a fixture which contains an invalid object
+        outputs an error message which contains the pk of the object
+        that triggered the error.
+        """
+        new_io = StringIO.StringIO()
+        management.call_command('loaddata', 'invalid.json', verbosity=0, 
stderr=new_io, commit=False)
+        output = new_io.getvalue().strip().split('\n')
+        self.assertRegexpMatches(output[-1], "IntegrityError: Could not load 
fixtures.Article\(pk=1\): .*$")
+
     def test_loading_using(self):
         # Load db fixtures 1 and 2. These will load using the 'default' 
database identifier explicitly
         management.call_command('loaddata', 'db_fixture_1', verbosity=0, 
using='default', commit=False)

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to