and the model setup:

class Policy(models.Model):
    policy_number = models.CharField(max_length=10)

class Version(models.Model):
    policy = models.ForeignKey(Policy)

class Location(models.Model):
    version = models.ForeignKey(Version, blank=True, null=True)

class Item(models.Model):
    version = models.ForeignKey(Version)
    location = models.ForeignKey(Location, blank=True, null=True)

class ItemRateCode(models.Model):
    item = models.ForeignKey(Item)

class PropertyItem(models.Model):
    item_rate_code = models.ForeignKey(ItemRateCode)

class Coverage(models.Model):
    version = models.ForeignKey(Version)
    item_rate_code = models.ForeignKey(ItemRateCode)


And to see it fail:
*tests.py*

from django.test import TestCase

from myapp.models import Policy, Version, Location, Item
from myapp.models import ItemRateCode, PropertyItem, Coverage

class DeletePolicyTests(TestCase):

    def setup_models(self):

        policy = Policy.objects.create(pk=1, policy_number="1234")
        version = Version.objects.create(policy=policy)
        location = Location.objects.create(version=version)

        item1 = Item.objects.create(version=version, location=location)
        item2 = Item.objects.create(version=version, location=location)

        # one for each item
        item_rate_code1 = ItemRateCode.objects.create(item=item1)
        item_rate_code2 = ItemRateCode.objects.create(item=item2)

        # one for each item_rate_code
        Coverage.objects.create(version=version,
item_rate_code=item_rate_code1)
        Coverage.objects.create(version=version,
item_rate_code=item_rate_code2)

        # one for each item_rate_code
        PropertyItem.objects.create(item_rate_code=item_rate_code1)
        PropertyItem.objects.create(item_rate_code=item_rate_code2)

    def test_deletes_policy_successfully(self):
        self.setup_models()
        policy = Policy.objects.get(pk=1)
        self.assertEqual(None, policy.delete())
        self.assertEqual(0, len(Policy.objects.all()))

On Wed, Apr 6, 2011 at 2:41 PM, Aaron Madison <[email protected]>wrote:

> I'm not sure if attaching files straight through email will work or not...
> but I'll give it a go. The attached zip file is a sample project that
> re-creates the situation.
> The output is as follows:
>
> (delete_bug)amadison@dev-aaron:~/projects/delete_bug/project$ ./manage.py
> test myapp
> Creating test database for alias 'default'...
> E
> ======================================================================
> ERROR: test_deletes_policy_successfully (myapp.tests.DeletePolicyTests)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "/home/amadison/projects/delete_bug/project/myapp/tests.py", line
> 34, in test_deletes_policy_successfully
>     self.assertEqual(None, policy.delete())
>   File
> "/home/amadison/projects/delete_bug/lib/python2.6/site-packages/django/db/models/base.py",
> line 581, in delete
>     collector.delete()
>   File
> "/home/amadison/projects/delete_bug/lib/python2.6/site-packages/django/db/models/deletion.py",
> line 63, in decorated
>     func(self, *args, **kwargs)
>   File
> "/home/amadison/projects/delete_bug/lib/python2.6/site-packages/django/db/models/deletion.py",
> line 254, in delete
>     query.delete_batch(pk_list, self.using)
>   File
> "/home/amadison/projects/delete_bug/lib/python2.6/site-packages/django/db/models/sql/subqueries.py",
> line 44, in delete_batch
>     self.do_query(self.model._meta.db_table, where, using=using)
>   File
> "/home/amadison/projects/delete_bug/lib/python2.6/site-packages/django/db/models/sql/subqueries.py",
> line 29, in do_query
>     self.get_compiler(using).execute_sql(None)
>   File
> "/home/amadison/projects/delete_bug/lib/python2.6/site-packages/django/db/models/sql/compiler.py",
> line 735, in execute_sql
>     cursor.execute(sql, params)
>   File
> "/home/amadison/projects/delete_bug/lib/python2.6/site-packages/django/db/backends/mysql/base.py",
> line 86, in execute
>     return self.cursor.execute(query, args)
>   File
> "/home/amadison/projects/delete_bug/lib/python2.6/site-packages/MySQLdb/cursors.py",
> line 174, in execute
>     self.errorhandler(self, exc, value)
>   File
> "/home/amadison/projects/delete_bug/lib/python2.6/site-packages/MySQLdb/connections.py",
> line 36, in defaulterrorhandler
>     raise errorclass, errorvalue
> IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key
> constraint fails (`test_delete_test`.`myapp_item`, CONSTRAINT
> `version_id_refs_id_15106961` FOREIGN KEY (`version_id`) REFERENCES
> `myapp_version` (`id`))')
>
> ----------------------------------------------------------------------
> Ran 1 test in 0.120s
>
>
>
>
> On Wed, Apr 6, 2011 at 2:06 PM, Jacob Kaplan-Moss <[email protected]>wrote:
>
>> On Wed, Apr 6, 2011 at 11:14 AM, amadison <[email protected]>
>> wrote:
>> > It you run the test case against django 1.3 it blows up... in django
>> > 1.2.5 it passes.
>>
>> Just a quick process point: it helps a lot if you post the actual
>> error you're seeing, traceback and all.
>>
>> Unless Django's literally blowing up your computer, in which case I
>> suggest you stop buying the TNT-powered hard drives.
>>
>> Jacob
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>>
>

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

Reply via email to