Ticket #23076: delete_fails_with_m2m_to_proxy_model.patch

File delete_fails_with_m2m_to_proxy_model.patch, 3.4 KB (added by James Murty, 5 years ago)

Patch file for Django PR #5399

  • tests/delete_regress/models.py

    diff --git a/tests/delete_regress/models.py b/tests/delete_regress/models.py
    index 6afe841..a641145 100644
    a b class OrderedPerson(models.Model): 
    139139
    140140    class Meta:
    141141        ordering = ['name']
     142
     143
     144# Models for #23076
     145
     146
     147class ConcretePhoto(Photo):
     148    pass
     149
     150
     151class M2MtoFile(models.Model):
     152    # M2M to concrete proxy base
     153    my_files = models.ManyToManyField(File)
     154
     155
     156class M2MtoImage(M2MtoFile):
     157    # M2M to proxy
     158    my_images = models.ManyToManyField(Image)
     159
     160
     161class M2MtoPhoto(M2MtoImage):
     162    # M2M to proxy of proxy
     163    my_photos = models.ManyToManyField(Photo)
     164
     165
     166class M2MtoConcretePhoto(M2MtoPhoto):
     167    # M2M to concrete proxy child
     168    my_concrete_photos = models.ManyToManyField(ConcretePhoto)
  • tests/delete_regress/tests.py

    diff --git a/tests/delete_regress/tests.py b/tests/delete_regress/tests.py
    index e1cbe70..c3031d2 100644
    a b from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature 
    1010from .models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith,
    1111    PlayedWithNote, Email, Researcher, Food, Eaten, Policy, Version, Location,
    1212    Item, Image, File, Photo, FooFile, FooImage, FooPhoto, FooFileProxy, Login,
    13     OrgUnit, OrderedPerson, House)
     13    OrgUnit, OrderedPerson, House, M2MtoConcretePhoto, ConcretePhoto)
    1414
    1515
    1616# Can't run this test under SQLite, because you can't
    class ProxyDeleteTest(TestCase): 
    269269            Image.objects.values_list().delete()
    270270
    271271
     272# Error conditions don't show up under SQLite, needs FK constraints in DB
     273@skipUnlessDBFeature('supports_foreign_keys')
     274class ProxyDeleteM2MRelationshipsTest(TransactionTestCase):
     275    """
     276    Test that reverse FK component of M2M relationship to proxy models is
     277    deleted when the relevant target model is deleted, to avoid errors like the
     278    following in DBs that enforce FK integrity constraints:
     279
     280    - PostgreSQL:
     281        IntegrityError: update or delete on table "TargetTable" violates
     282        foreign key constraint "FKConstraint" on table "M2MThroughTable"
     283        DETAIL:  Key (id)=(6) is still referenced from table "M2MThroughTable".
     284    """
     285
     286    available_apps = ['delete_regress']
     287
     288    def setUp(self):
     289        self.concrete_photo = ConcretePhoto.objects.create()
     290        self.m2m_source = M2MtoConcretePhoto.objects.create()
     291
     292    def test_delete_with_m2m_to_concrete_base(self):
     293        self.m2m_source.my_files.add(self.concrete_photo)
     294
     295        self.concrete_photo.delete()
     296        self.assertEqual(len(self.m2m_source.my_files.all()), 0)
     297
     298    def test_delete_with_m2m_to_proxy(self):
     299        self.m2m_source.my_images.add(self.concrete_photo)
     300
     301        self.concrete_photo.delete()
     302        self.assertEqual(len(self.m2m_source.my_images.all()), 0)
     303
     304    def test_delete_with_m2m_to_proxy_of_proxy(self):
     305        self.m2m_source.my_photos.add(self.concrete_photo)
     306
     307        self.concrete_photo.delete()
     308        self.assertEqual(len(self.m2m_source.my_photos.all()), 0)
     309
     310    def test_delete_with_m2m_to_concrete_proxy_child(self):
     311        self.m2m_source.my_concrete_photos.add(self.concrete_photo)
     312
     313        self.concrete_photo.delete()
     314        self.assertEqual(len(self.m2m_source.my_concrete_photos.all()), 0)
     315
     316
    272317class Ticket19102Tests(TestCase):
    273318    """
    274319    Test different queries which alter the SELECT clause of the query. We
Back to Top