Opened 6 months ago

Last modified 3 days ago

#36264 assigned Bug

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: Accepted
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 (9)

comment:1 by Sarah Boyce, 6 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, 6 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 6 months ago by Vladimir (previous) (diff)

comment:3 by Vladimir, 6 months ago

Resolution: invalid
Status: closednew

comment:4 by Sarah Boyce, 6 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, 6 months ago

Owner: set to JakeWalson
Status: newassigned

comment:6 by JaeHyuckSa, 6 days ago

Owner: changed from JakeWalson to JaeHyuckSa

comment:7 by JaeHyuckSa, 6 days ago

Has patch: set

comment:8 by Simon Charette, 3 days ago

Patch needs improvement: set

comment:9 by JaeHyuckSa, 3 days ago

Patch needs improvement: unset
Note: See TracTickets for help on using tickets.
Back to Top