makinacorpus / django-safedelete

Mask your objects instead of deleting them from your database.
https://django-safedelete.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
677 stars 122 forks source link

how does SOFT_DELETE_CASCADE work? #202

Open fcoppey opened 2 years ago

fcoppey commented 2 years ago

let’s say I have 2 models A and B, B has a foreign key pointing to A and other have soft delete with policy SOFT_DELETE_CASCADE.

If I soft delete an object in A, then the related object in B gets soft deleted as well BUT it seems its foreign key field point to A is set to NULL... which loses the link between object A and object B when I later want to undelete object A.

Is it the intended behaviour?

odin1in commented 1 year ago

I have the same issue. After I check the Django & safedelete source code, this is because you set the Null=True in your model.

https://docs.djangoproject.com/en/3.2/_modules/django/db/models/deletion/

https://github.com/makinacorpus/django-safedelete/blob/179288b840205b827570ec3db1d1365116155e6e/safedelete/models.py#L244-L274

fcoppey commented 1 year ago

this is quit annoying as I must keep Null=True for some models, is there a workaround?

fcoppey commented 1 year ago

I’ve found a workaround by saving the pk list of related soft deleted objects before the soft deletion and then reassigning the parent id to these after the soft deletion... quite cumbersome. is any willing to make a patch for this bug? my level of coding is not high enough unfortunately... @Gagaro ?

Gagaro commented 1 year ago

Indeed I'm not sure how to fix that, maybe we should have our own CASCADE which does not set the other field as null.

I'll gladly review a PR if someone is willing to try and patch this.

tuky commented 1 year ago

Having this would be great. For now we are stuck with _safedelete_policy = SOFT_DELETE. Also we have a regular model R that is related to a SafeDeleteModel S via ForeignKey. And this regular model R should be hard deleted, if the related instance of S is hard deleted and if S is soft deleted, R should not be touched.