Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Deleting correspondent cascades the delete to all assigned documents #235

Closed
thomasbrueggemann opened this issue Jun 5, 2017 · 11 comments
Closed

Comments

@thomasbrueggemann
Copy link
Contributor

Not a bug report, but I was quite surprised that when I deleted a correspondent all documents assigned to that correspondent were not available anymore. This could potentially lead to some unexpected/unintended behaviour.

I just wanted to get rid of the correspondent - I like to only have some major correspondents listed and not every document has a correspondent assigned.

What do you think?

@ddddavidmartin
Copy link
Contributor

I agree.

Also, is there currently a way of deleting correspondents without removing the documents as well?

@danielquinn
Copy link
Collaborator

That seems like reasonable behaviour. Django's default behaviour is to cascade deletes, which is why this is happening, but it can be prevented with an argument to to the field definition.

If one of you wants to issue a PR with this (and a test!) then I'll merge it. Otherwise I'll try to get to this soon.

@ddddavidmartin
Copy link
Contributor

I had a quick look at this, and applying @danielquinn's approach would be as follows and it seems to work fine on my end.

diff --git a/src/documents/models.py b/src/documents/models.py
index 54f45ae..bc34eac 100644
--- a/src/documents/models.py
+++ b/src/documents/models.py
@@ -172,7 +172,8 @@ class Document(models.Model):
     TYPES = (TYPE_PDF, TYPE_PNG, TYPE_JPG, TYPE_GIF, TYPE_TIF,)
 
     correspondent = models.ForeignKey(
-        Correspondent, blank=True, null=True, related_name="documents")
+        Correspondent, blank=True, null=True, related_name="documents",
+        on_delete=models.SET_NULL)
 
     title = models.CharField(max_length=128, blank=True, db_index=True)
 

See https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey.on_delete.

I'm not sure yet about an appropriate test though.

@danielquinn
Copy link
Collaborator

Thanks for your patience on this guys. I've written a fix (and a corresponding test) and it's now in master.

@thomasbrueggemann
Copy link
Contributor Author

Thanks, Daniel!

@HorayNarea
Copy link

The same issue is (still) present with tags

@danielquinn
Copy link
Collaborator

That's odd, since an n:1 relationship (like documents have with tags) should never result in a cascade delete. I'll re-open this and see if I can write a test for it.

@danielquinn danielquinn reopened this Jul 17, 2017
@ddddavidmartin
Copy link
Contributor

It is phrased a bit oddly but on my setup deleting a tag just deletes the tag and its associations.

From the delete-a-tag dialog:

Summary
    Tags: 1
    Document-tag relationships: 2

Objects
    Tag: some_tag
        Document-tag relationship: Document_tags object
        Document-tag relationship: Document_tags object

It is deleting the respective assignment of the tags (the document-tag relationship) and the tag itself.

@danielquinn
Copy link
Collaborator

That's the expected behaviour. If you delete a tag, then it, and all associations with it should go, but the documents to which it was linked should not be removed. So am I to understand that this is a non-issue?

@HorayNarea
Copy link

@ddddavidmartin is right, i misinterpreted the warning. Sorry.

@danielquinn
Copy link
Collaborator

K cool. Re-closing now.

jayme-github pushed a commit to jayme-github/paperless that referenced this issue Jan 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants