Permalink
Browse files

[1.11.x] Fixed #29016 -- Fixed incorrect foreign key nullification on…

… related instance deletion.

Backport of 9a621ed from master
  • Loading branch information...
1 parent fb56038 commit 419705bbe84e27c3d5be85f198a0352a6724927e @Nimn Nimn committed with timgraham Jan 13, 2018
Showing with 31 additions and 4 deletions.
  1. +1 −1 django/db/models/deletion.py
  2. +2 −1 docs/releases/1.11.10.txt
  3. +2 −0 tests/delete_regress/models.py
  4. +26 −2 tests/delete_regress/tests.py
@@ -286,8 +286,8 @@ def delete(self):
# update fields
for model, instances_for_fieldvalues in six.iteritems(self.field_updates):
- query = sql.UpdateQuery(model)
for (field, value), instances in six.iteritems(instances_for_fieldvalues):
+ query = sql.UpdateQuery(model)
query.update_batch([obj.pk for obj in instances],
{field.name: value}, self.using)
@@ -9,4 +9,5 @@ Django 1.11.10 fixes several bugs in 1.11.9.
Bugfixes
========
-* ...
+* Fixed incorrect foreign key nullification if a model has two foreign keys to
+ the same model and a target model is deleted (:ticket:`29016`).
@@ -56,6 +56,8 @@ class Email(Contact):
class Researcher(models.Model):
contacts = models.ManyToManyField(Contact, related_name="research_contacts")
+ primary_contact = models.ForeignKey(Contact, models.SET_NULL, null=True, related_name='primary_contacts')
+ secondary_contact = models.ForeignKey(Contact, models.SET_NULL, null=True, related_name='secondary_contacts')
class Food(models.Model):
@@ -6,7 +6,7 @@
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
from .models import (
- Award, AwardNote, Book, Child, Eaten, Email, File, Food, FooFile,
+ Award, AwardNote, Book, Child, Contact, Eaten, Email, File, Food, FooFile,
FooFileProxy, FooImage, FooPhoto, House, Image, Item, Location, Login,
OrderedPerson, OrgUnit, Person, Photo, PlayedWith, PlayedWithNote, Policy,
Researcher, Toy, Version,
@@ -335,7 +335,7 @@ def test_ticket_19102_defer(self):
self.assertTrue(Login.objects.filter(pk=self.l2.pk).exists())
-class OrderedDeleteTests(TestCase):
+class DeleteTests(TestCase):
def test_meta_ordered_delete(self):
# When a subquery is performed by deletion code, the subquery must be
# cleared of all ordering. There was a but that caused _meta ordering
@@ -345,3 +345,27 @@ def test_meta_ordered_delete(self):
OrderedPerson.objects.create(name='Bob', lives_in=h)
OrderedPerson.objects.filter(lives_in__address='Foo').delete()
self.assertEqual(OrderedPerson.objects.count(), 0)
+
+ def test_foreign_key_delete_nullifies_correct_columns(self):
+ """
+ With a model (Researcher) that has two foreign keys pointing to the
+ same model (Contact), deleting an instance of the target model
+ (contact1) nullifies the correct fields of Researcher.
+ """
+ contact1 = Contact.objects.create(label='Contact 1')
+ contact2 = Contact.objects.create(label='Contact 2')
+ researcher1 = Researcher.objects.create(
+ primary_contact=contact1,
+ secondary_contact=contact2,
+ )
+ researcher2 = Researcher.objects.create(
+ primary_contact=contact2,
+ secondary_contact=contact1,
+ )
+ contact1.delete()
+ researcher1.refresh_from_db()
+ researcher2.refresh_from_db()
+ self.assertIsNone(researcher1.primary_contact)
+ self.assertEqual(researcher1.secondary_contact, contact2)
+ self.assertEqual(researcher2.primary_contact, contact2)
+ self.assertIsNone(researcher2.secondary_contact)

0 comments on commit 419705b

Please sign in to comment.