Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.3.X] Fixed #15776 - delete regression in Django 1.3 involving null…

…able foreign keys

Many thanks to aaron.l.madison for the detailed report and to emulbreh for
the fix.

Backport of [16295] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.3.X@16296 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6e87dacf62e742b78787659b9a51561fd5465682 1 parent 4124ef3
Luke Plant authored May 30, 2011
6  django/db/models/deletion.py
@@ -83,8 +83,8 @@ def __init__(self, using):
83 83
     def add(self, objs, source=None, nullable=False, reverse_dependency=False):
84 84
         """
85 85
         Adds 'objs' to the collection of objects to be deleted.  If the call is
86  
-        the result of a cascade, 'source' should be the model that caused it
87  
-        and 'nullable' should be set to True, if the relation can be null.
  86
+        the result of a cascade, 'source' should be the model that caused it,
  87
+        and 'nullable' should be set to True if the relation can be null.
88 88
 
89 89
         Returns a list of all objects that were not already collected.
90 90
         """
@@ -100,7 +100,7 @@ def add(self, objs, source=None, nullable=False, reverse_dependency=False):
100 100
         # Nullable relationships can be ignored -- they are nulled out before
101 101
         # deleting, and therefore do not affect the order in which objects have
102 102
         # to be deleted.
103  
-        if new_objs and source is not None and not nullable:
  103
+        if source is not None and not nullable:
104 104
             if reverse_dependency:
105 105
                 source, model = model, source
106 106
             self.dependencies.setdefault(source, set()).add(model)
16  tests/regressiontests/delete_regress/models.py
@@ -51,3 +51,19 @@ class Food(models.Model):
51 51
 class Eaten(models.Model):
52 52
     food = models.ForeignKey(Food, to_field="name")
53 53
     meal = models.CharField(max_length=20)
  54
+
  55
+
  56
+# Models for #15776
  57
+
  58
+class Policy(models.Model):
  59
+    policy_number = models.CharField(max_length=10)
  60
+
  61
+class Version(models.Model):
  62
+    policy = models.ForeignKey(Policy)
  63
+
  64
+class Location(models.Model):
  65
+    version = models.ForeignKey(Version, blank=True, null=True)
  66
+
  67
+class Item(models.Model):
  68
+    version = models.ForeignKey(Version)
  69
+    location = models.ForeignKey(Location, blank=True, null=True)
10  tests/regressiontests/delete_regress/tests.py
@@ -5,7 +5,8 @@
5 5
 from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
6 6
 
7 7
 from models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith,
8  
-    PlayedWithNote, Contact, Email, Researcher, Food, Eaten)
  8
+    PlayedWithNote, Contact, Email, Researcher, Food, Eaten,
  9
+    Policy, Version, Location, Item)
9 10
 
10 11
 
11 12
 # Can't run this test under SQLite, because you can't
@@ -102,6 +103,13 @@ def test_fk_to_m2m_through(self):
102 103
         # first two asserts just sanity checks, this is the kicker:
103 104
         self.assertEqual(PlayedWithNote.objects.count(), 0)
104 105
 
  106
+    def test_15776(self):
  107
+        policy = Policy.objects.create(pk=1, policy_number="1234")
  108
+        version = Version.objects.create(policy=policy)
  109
+        location = Location.objects.create(version=version)
  110
+        item = Item.objects.create(version=version, location=location)
  111
+        policy.delete()
  112
+
105 113
 
106 114
 class DeleteCascadeTransactionTests(TransactionTestCase):
107 115
     def test_inheritance(self):

0 notes on commit 6e87dac

Please sign in to comment.
Something went wrong with that request. Please try again.