Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on_delete=models.SET_NULL is not working as expected #161

Open
sudheeshshivan opened this issue Jul 22, 2021 · 5 comments
Open

on_delete=models.SET_NULL is not working as expected #161

sudheeshshivan opened this issue Jul 22, 2021 · 5 comments

Comments

@sudheeshshivan
Copy link

class Asset(SafeDeleteModel):
    _safedelete_policy = 2
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    asset_id = models.CharField(max_length=100)
class Claim(SafeDeleteModel):
    _safedelete_policy = 2
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    asset = models.ForeignKey(Asset, on_delete=models.SET_NULL, related_name="claim_asset", null=True, blank=True)

I added a row in Asset and which is referenced in Claim. Then I delete that asset, but the asset reference in the Claim model is not set to null and it is still referencing the safe deleted asset.
Is there any other configuration required to make this work..?

@Gagaro
Copy link
Member

Gagaro commented Aug 9, 2021

The on_delete is used only when the instance is actually deleted. Because your model is a safedelete one, it won't trigger that.

You need to overwrite the delete method of your model to set the related instances attribute.

If you want to actually delete it (and trigger the on_delete), you have to hard delete it: asset.delete(force_policy=HARD_DELETE).

@fcoppey
Copy link

fcoppey commented Mar 25, 2022

I’m using this policy: _safedelete_policy = SOFT_DELETE_CASCADE

I have two models A and B with safe delete, the first one being a foreign key of the second one, when I delete an object in A the foreign key in B is set to null apparently. Is it a normal behavoir? because when I undelete the object in A I just can’t find the object in B anymore...

@Gagaro
Copy link
Member

Gagaro commented Apr 26, 2022

B may be still deleted. There was a recent contribution which also undelete (#197) which also undelete linked objects. It is not released yet so you can try it with the master version.

@Gagaro
Copy link
Member

Gagaro commented Apr 26, 2022

Actually I just released it in 1.2.0, you can test it with this version 😉 .

@fcoppey
Copy link

fcoppey commented Apr 26, 2022

awesome thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants