Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#27407 closed Bug (fixed)

Model.delete(keep_parents=True) should preserve parent reverse relationships

Reported by: Simon Charette Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The keep_parent option of Model.delete() is documented to keep the parent model’s data but actually only preserves the child-parent relationship and will discard the parent reverse relationships which can lead to unexpected data loss.

class Place(models.Model):
    pass

class Restaurant(Place):
    pass

class Event(models.Model):
    location = models.ForeignKey(Place, related_name='events')

>> restaurant = Restaurant.objects.create()
>> place = restaurant.parent_ptr
>> Events.objects.create(location=place)
>> place.events.count()
1
>> restaurant.delete(keep_parents=True)
>> place.events.count()
0

I believe that users of the keep_parents option would expect relationships pointing to their kept parents to also be preserved as they were not deleted and following the CASCADE chain just doesn't make sense here.

If this had been discovered during the 1.9.x life cycle this could have qualified for a potential data loss backport but I'm not sure what should be done at this point.

The keep_parents option was added in 81c2d9f60b9206c1291e5b1c3c8686f24a7726e1 to fix the long standing #15579.

Change History (6)

comment:1 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

1.9.x still qualifies for data loss fixes, though I'm not sure it's critical since no one noticed it until now. Fine with me for at least 1.10.x.

comment:2 by Tim Graham, 7 years ago

Summary: The delete(keep_parents=True) option should preserve parent reverse relationships.Model.delete(keep_parents=True) should preserve parent reverse relationships

comment:3 by Simon Charette, 7 years ago

Has patch: set
Version: master1.9

comment:4 by Tim Graham, 7 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Simon Charette <charette.s@…>, 7 years ago

Resolution: fixed
Status: newclosed

In 31a56e30:

Fixed #27407 -- Made Model.delete(keep_parents=True) preserve parent reverse relationships.

Thanks Tim for the review.

comment:6 by Simon Charette <charette.s@…>, 7 years ago

In b495d8e3:

[1.10.x] Fixed #27407 -- Made Model.delete(keep_parents=True) preserve parent reverse relationships.

Thanks Tim for the review.

Backport of 31a56e30cfb3c8e1a9373e5abdd12b5b23d5d910 from master

Note: See TracTickets for help on using tickets.
Back to Top