question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid result in many to many list if through instance is soft deleted

See original GitHub issue

Hi guys. I have below models

class BaseModel(SafeDeleteModel):
     _safedelete_policy = SOFT_DELETE_CASCADE
     
class Student(BaseModel):
    full_name = models.CharField(max_length=150)

class School(BaseModel):
    name = models.CharField(max_length=150)
    students = models.ManyToManyField(
        Student,
        through='SchoolStudent',
    )

class SchoolStudent(BaseModel):
    student = models.ForeignKey(Student, on_delete=models.CASCADE)
    school = models.ForeignKey(School, on_delete=models.CASCADE)

I create a relationship between a school and a student, and then I delete it. but the below event occurs

school = School.objects.create(name='new_school')
student = Student.objects.create(full_name='Tom Smith')
school.students.add(student)
# delete all relationship
SchoolStudent.objects.all().delete()

school.students.all()

<QuerySet [<Student: Tom Smith>]>

As you see. we get wrong results. we deleted the relationship but we get the queryset. Have you got any idea to solve the issue?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bjudsoncommented, Oct 18, 2021

I’m seeing this same issue with 1.1.0. I actually was working on my own soft delete implementation and this was one of the issues that led me to start looking at 3rd party libraries. What I’ve found is that RelatedManager inherits the way you’d expect, but ManyRelatedManager does not. So you’ll find with an example like above:

school.students.count()  # returns 1
school.schoolstudent_set.count()  # returns 0

Thanks for your response. It worked, but as you know, it returns the SchoolStudent entities. but I need student entities.

Correct. As a workaround for your situation, you can just do something like:

Student.objects.filter(schoolstudent__school=school, schoolstudent__deleted=None)

But it will be difficult to remember not to use ManyRelatedManager with these objects, and likely lead to bugs at some point.

I’ll keep looking at this and see if I can find any solution, and if so submit a PR. I decided to adopt this library in a project I’m working on, largely because of its strong tests and handling of tricky situations like this, so I’d love to have this one handled in an intuitive way.

Thanks for your work & responsiveness @Gagaro

0reactions
Gagarocommented, Oct 18, 2021

So because create_forward_many_to_many_manager() create a new manager inheriting from our own and not the other way around, it seems complicated to fix this.

I added a test which reproduce this but it fails for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hibernate left join fetch to soft-deleted entity mapped with ...
Hibernate left join fetch to soft-deleted entity mapped with @ManyToMany association generates invalid query?
Read more >
Soft delete with EF Core - Medium
In this article you will learn how to implement soft delete capability in your application and how would it impact on querying of...
Read more >
How to Implement a Soft Delete with Spring JPA - Baeldung
A common way to implement soft delete is to add a field that will indicate whether data has been deleted or not. This...
Read more >
Working With Soft Deletes in Laravel 8 and 9 (By Example)
The annoying thing about deleting data from a database is that it's gone forever. We can't even look at the data to see...
Read more >
EF Core In depth – Soft deleting data with Global Query Filters
This type of feature is known as soft delete and it has many good ... For instance, here is a Book entity with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found