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

Fixed an infinite recursion bug when deleting models related to a tracked model with a ForeignKey #620

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nick-prat
Copy link

@nick-prat nick-prat commented May 23, 2024

When deleting a model, that has a reverse relationship to a tracked model with SET_NULL on_delete

Problem

This issue arrises on these conditions:

  1. You delete a model that is target of a foreign key, with on_delete=SET_NULL (I believe also with PROTECT)
  2. The related model that is going to be deleted has a field tracker that tracks a foreign key field
    When this happens, django loads the instance from the db with all fields deferred other than id. In this code,
def current(self, fields=None):
    if fields is None:
        deferred_fields = self.deferred_fields
        if deferred_fields:
            fields = [
                field for field in self.fields
                if field not in deferred_fields
            ]
        else:
            fields = self.fields
    
    return {f: self.get_field_value(f) for f in fields}

we skip those deferred fields, but this is where the issue arrises, we don't check self.field_map[field] here, so even though the FK is deferred, we still attemp to load it, as we check for, as an example, foo, while the deffered field is foo_id.

Issue talked about here as well: #533

…erse relationship to a tracked model with SET_NULL on_delete
@nick-prat nick-prat changed the title Fixed an infinite recursion bug Fixed an infinite recursion bug when deleting models related to a tracked model with a ForeignKey May 23, 2024
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

Successfully merging this pull request may close these issues.

1 participant