Ticket #16026: 16026_loaddata_error.diff
File 16026_loaddata_error.diff, 3.5 KB (added by , 13 years ago) |
---|
-
tests/modeltests/fixtures/fixtures/invalid.json
1 [ 2 { 3 "pk": "1", 4 "model": "fixtures.article", 5 "fields": { 6 "headline": null, 7 "pub_date": "2006-06-16 13:00:00" 8 } 9 } 10 ] -
tests/modeltests/fixtures/tests.py
252 252 '<Article: Python program becomes self aware>' 253 253 ]) 254 254 255 def test_loaddata_error_message(self): 256 """ 257 Verifies that loading a fixture which contains an invalid object 258 outputs an error message which contains the pk of the object 259 that triggered the error. 260 """ 261 new_io = StringIO.StringIO() 262 management.call_command('loaddata', 'invalid.json', verbosity=0, stderr=new_io, commit=False) 263 output = new_io.getvalue().strip().split('\n') 264 self.assertRegexpMatches(output[-1], "IntegrityError: Could not load fixtures.Article\(pk=1\): .*$") 265 255 266 def test_loading_using(self): 256 267 # Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly 257 268 management.call_command('loaddata', 'db_fixture_1', verbosity=0, using='default', commit=False) -
django/core/management/commands/loaddata.py
12 12 from django.core import serializers 13 13 from django.core.management.base import BaseCommand 14 14 from django.core.management.color import no_style 15 from django.db import connections, router, transaction, DEFAULT_DB_ALIAS 15 from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS, 16 IntegrityError, DatabaseError) 16 17 from django.db.models import get_apps 17 18 from django.utils.itercompat import product 18 19 … … 177 178 if router.allow_syncdb(using, obj.object.__class__): 178 179 loaded_objects_in_fixture += 1 179 180 models.add(obj.object.__class__) 180 obj.save(using=using) 181 try: 182 obj.save(using=using) 183 except (DatabaseError, IntegrityError), e: 184 msg = "Could not load %(app_label)s.%(object_name)s(pk=%(pk)s): %(error_msg)s" % { 185 'app_label': obj.object._meta.app_label, 186 'object_name': obj.object._meta.object_name, 187 'pk': obj.object.pk, 188 'error_msg': e 189 } 190 raise e.__class__, e.__class__(msg), sys.exc_info()[2] 181 191 182 192 # Since we disabled constraint checks, we must manually check for 183 193 # any invalid keys that might have been added