jazzband / django-polymorphic

Improved Django model inheritance with automatic downcasting
https://django-polymorphic.readthedocs.io
Other
1.61k stars 275 forks source link

CASCADE deletion of model with nested polymorphic models fail with xxx_ptr does not exist error #608

Open DanSheps opened 3 weeks ago

DanSheps commented 3 weeks ago

I have the following mdels:


class PolyDevice(Model):
    name = CharField(max_length=64)

class NotPolyInterface(PolymorphicModel):
    name = CharField(max_length=64)
    device = ForeignKey(to=PolyDevice, on_delete=CASCADE)
    ethernety_stuff = CharField(max_length=64)
    modular_stuff = CharField(max_length=64)
    fixed_stuff = CharField(max_length=64)
    wirelessy_stuff = CharField(max_length=64)
    fc_stuff = CharField(max_length=64)

class PolyInterface(PolymorphicModel):
    name = CharField(max_length=64)
    device = ForeignKey(to=PolyDevice, on_delete=CASCADE)

class PolyEthernetInterface(PolyInterface):
    ethernety_stuff = CharField(max_length=64)

class PolyModularInterface(PolyEthernetInterface):
    modular_stuff = CharField(max_length=64)

class PolyFixedInterface(PolyEthernetInterface):
    fixed_stuff = CharField(max_length=64)

class PolyWirelessInterface(PolyInterface):
    wirelessy_stuff = CharField(max_length=64)

class PolyFCInterface(PolyInterface):
    fc_stuff = CharField(max_length=64)

When I initialize a device and then initialize one of each type of interface (Fixed, Modular, Wireless, FC) and then attempt to delete the device, I receive the following error:

>>> PolyDevice.objects.first().delete()
Traceback (most recent call last):
  File "C:\Users\dan\AppData\Local\Programs\Python\Python310\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "C:\Development\test\venv\lib\site-packages\django\db\models\base.py", line 1131, in delete
    collector.collect([self], keep_parents=keep_parents)
  File "C:\Development\test\venv\lib\site-packages\django\db\models\deletion.py", line 350, in collect
    on_delete(self, field, sub_objs, self.using)
  File "C:\Development\test\venv\lib\site-packages\django\db\models\deletion.py", line 23, in CASCADE
    collector.collect(
  File "C:\Development\test\venv\lib\site-packages\django\db\models\deletion.py", line 298, in collect
    parent_objs = [getattr(obj, ptr.name) for obj in new_objs]
  File "C:\Development\test\venv\lib\site-packages\django\db\models\deletion.py", line 298, in <listcomp>
    parent_objs = [getattr(obj, ptr.name) for obj in new_objs]
AttributeError: 'PolyWirelessInterface' object has no attribute 'polyethernetinterface_ptr'. Did you mean: 'polyethernetinterface'?