1
0
mirror of https://github.com/django/django.git synced 2024-12-23 09:36:06 +00:00

Fixed #16026 -- loaddata now identifies which object triggered an error.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16873 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Paul McMillan 2011-09-22 04:30:20 +00:00
parent 0781ed8ec8
commit 1d96d886e7
2 changed files with 23 additions and 2 deletions

View File

@ -12,7 +12,8 @@ from django.conf import settings
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 @@ class Command(BaseCommand):
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

View File

@ -252,6 +252,17 @@ class FixtureLoadingTests(TestCase):
'<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)