10 | 10 | from .models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith, |
11 | 11 | PlayedWithNote, Email, Researcher, Food, Eaten, Policy, Version, Location, |
12 | 12 | Item, Image, File, Photo, FooFile, FooImage, FooPhoto, FooFileProxy, Login, |
| 272 | # Error conditions don't show up under SQLite, needs FK constraints in DB |
| 273 | @skipUnlessDBFeature('supports_foreign_keys') |
| 274 | class 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 | |