tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.68k stars 390 forks source link

on_delete=CASCADE doesn't fire pre_delete and post_delete signals #1447

Open khamitovdr opened 1 year ago

khamitovdr commented 1 year ago

tortoise-orm==0.19.3

Setup

class FileBaseModel(models.Model):
    host = fields.ForeignKeyField(
        "models.Equipment", related_name="files", on_delete=fields.CASCADE, null=True
    )

@pre_delete(FileBaseModel)
async def equipment_media_pre_delete(
    sender: Type[FileBaseModel],
    file: FileBaseModel,
    using_db: Optional[BaseDBAsyncClient],
) -> None:
    log.info(f"pre_delete signal for file {file}")

Code

equipment = await Equipment.create(...)

file = await FileBaseModel.create(host=equipment)
await file.delete()
#### INFO:     pre_delete signal for file <FileBaseModel>

file = await FileBaseModel.create(host=equipment)
await equipment.delete()
#### Nothing happens! No logs...
Abdeldjalil-H commented 1 year ago

@denis240997 I think this is the expected behavior. The pre_delete signal is registered on FileBaseModel, not on Equipment.