Opened 7 months ago

Closed 3 weeks ago

Last modified 3 weeks ago

#36264 closed Bug (fixed)

keep_parents=True does not exclude proxy neighbors

Reported by: Vladimir Owned by: JaeHyuckSa
Component: Database layer (models, ORM) Version: 5.1
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

class Foo(Model):
    pass

class Bar(Foo):
    pass

class Baz(Foo):
    class Meta:
        proxy = True

class BazItem(Model):
    baz = ForeignKey(to=Baz, on_delete=CASCADE)

...

bar = Bar.objects.create()
baz = Baz.objects.all().first()
BazItem.objects.create(baz=baz)
assert BazItem.objects.all().count() == 1
bar.delete(keep_parents=True)
assert BazItem.objects.all().count() == 0

seems django.db.models.deletion.Collector.collect

if keep_parents and related.model in model._meta.all_parents:
    continue

should use concrete_model

Change History (12)

comment:1 by Sarah Boyce, 7 months ago

Resolution: invalid
Status: newclosed

I'm not 100% sure what you're trying to show here:

foo_parent = bar.foo_ptr
bar.delete(keep_parents=True)
foo_parent.refresh_from_db()
assert foo

So the parent is kept. That the BazItem is deleted is due to the CASCADE delete. To me, this looks like it's by design.

in reply to:  1 comment:2 by Vladimir, 7 months ago

Replying to Sarah Boyce:

I'm not 100% sure what you're trying to show here:

foo_parent = bar.foo_ptr
bar.delete(keep_parents=True)
foo_parent.refresh_from_db()
assert foo

So the parent is kept. That the BazItem is deleted is due to the CASCADE delete. To me, this looks like it's by design.

BazItem referencing to Baz(Foo table), and deleting bar instance with keep_parents=True should delete only Bar table row, but delete BazItem too

If the Baz model also uses table inheritance, deleting bar with keep_parents=True will not affect the BazItem table. Regardless of where the BazItem will refer to, Foo or Baz

Last edited 7 months ago by Vladimir (previous) (diff)

comment:3 by Vladimir, 7 months ago

Resolution: invalid
Status: closednew

comment:4 by Sarah Boyce, 7 months ago

Triage Stage: UnreviewedAccepted

Apologies, I read it wrong and thought Baz inherited from Bar.
I can replicate, I agree this is a bug

comment:5 by JakeWalson, 7 months ago

Owner: set to JakeWalson
Status: newassigned

comment:6 by JaeHyuckSa, 7 weeks ago

Owner: changed from JakeWalson to JaeHyuckSa

comment:7 by JaeHyuckSa, 7 weeks ago

Has patch: set

comment:8 by Simon Charette, 6 weeks ago

Patch needs improvement: set

comment:9 by JaeHyuckSa, 6 weeks ago

Patch needs improvement: unset

comment:10 by Sarah Boyce, 3 weeks ago

Triage Stage: AcceptedReady for checkin

comment:11 by Sarah Boyce <42296566+sarahboyce@…>, 3 weeks ago

Resolution: fixed
Status: assignedclosed

In 748551f:

Fixed #36264 -- Excluded proxy neighbors of parents from deletion collection when keep_parents=True.

Signed-off-by: saJaeHyukc <wogur981208@…>

comment:12 by Sarah Boyce <42296566+sarahboyce@…>, 3 weeks ago

In e0f328d7:

[6.0.x] Fixed #36264 -- Excluded proxy neighbors of parents from deletion collection when keep_parents=True.

Signed-off-by: saJaeHyukc <wogur981208@…>

Backport of 748551fea0b4e37231203a063356572a47e09efb from main.

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