Ticket #16026: loaddata_error.diff

File loaddata_error.diff, 3.5 KB (added by Gabriel Hurley, 13 years ago)

Full patch with change, test, and additional intentionally invalid fixture.

  • django/core/management/commands/loaddata.py

     
    1212from django.core import serializers
    1313from django.core.management.base import BaseCommand
    1414from django.core.management.color import no_style
    15 from django.db import connections, router, transaction, DEFAULT_DB_ALIAS
     15from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS,
     16    IntegrityError, DatabaseError)
    1617from django.db.models import get_apps
    1718from django.utils.itercompat import product
    1819
     
    177178                                        if router.allow_syncdb(using, obj.object.__class__):
    178179                                            loaded_objects_in_fixture += 1
    179180                                            models.add(obj.object.__class__)
     181                                        try:
    180182                                            obj.save(using=using)
    181 
     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]
    182191                                # Since we disabled constraint checks, we must manually check for
    183192                                # any invalid keys that might have been added
    184193                                table_names = [model._meta.db_table for model in models]
  • 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

     
    252252            '<Article: Python program becomes self aware>'
    253253        ])
    254254
     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.assertEqual(output[-1], "IntegrityError: Could not load fixtures.Article(pk=1): fixtures_article.headline may not be NULL")
     265
    255266    def test_loading_using(self):
    256267        # Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly
    257268        management.call_command('loaddata', 'db_fixture_1', verbosity=0, using='default', commit=False)
Back to Top