Generic-related objects are cascade-deleted, but the cascade does not extend beyond them
|Reported by:||Carl Meyer||Owned by:||Carl Meyer|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Say I have these models:
class Award(models.Model): name = models.CharField(max_length=25) object_id = models.PositiveIntegerField() content_type = models.ForeignKey(ContentType) content_object = generic.GenericForeignKey() class AwardNote(models.Model): award = models.ForeignKey(Award) note = models.CharField(max_length=100) class Person(models.Model): name = models.CharField(max_length=25) awards = generic.GenericRelation(Award)
If I create one of each, then delete the Person, Django currently cascade-deletes the generically-related Award, but does not follow the relations any further, and thus the AwardNote is left undeleted. Databases which enforce referential integrity may delete the AwardNote themselves, or raise an error; databases which don't will leave the AwardNote with a broken ForeignKey.
Since Django's deletion behavior is advertised as equivalent to ON DELETE CASCADE, Django should explicitly delete the AwardNote in this case.
Currently deletion of generically-related objects is done in DeleteQuery.delete_batch_related(). I think the fix for this will involve moving it up to Model._collect_sub_objects() instead.
Change History (13)
comment:1 Changed 7 years ago by
|Component:||Uncategorized → Database layer (models, ORM)|
|Version:||1.1 → SVN|