Opened 22 minutes ago
#37508 new Bug
FETCH_PEERS: a deleted peer breaks related object access on other peers
| Reported by: | Akshat Gupta | Owned by: | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 6.1 |
| Severity: | Normal | Keywords: | fetch modes, FETCH_PEERS |
| Cc: | Akshat Gupta | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
#37436 made FETCH_PEERS tolerate peers whose rows were deleted, but only for deferred fields. Related objects still have two problems.
1. A deleted peer makes other peers fail
Models:
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): title = models.CharField(max_length=100) author = models.ForeignKey(Author, models.CASCADE) class Tag(models.Model): content_type = models.ForeignKey(ContentType, models.CASCADE) object_id = models.PositiveIntegerField() content_object = GenericForeignKey()
book1, book2 = Book.objects.only("title").fetch_mode(FETCH_PEERS).order_by("pk") Book.objects.filter(pk=book1.pk).delete() # e.g. by another request book2.author.name
book2 and its author both exist, but this raises the error of the deleted peer:
Book.DoesNotExist: Book matching query does not exist.
Every later access raises it again, because the deleted peer stays in the peer list. With FETCH_ONE, book2.author.name returns 'Bob'. A GenericForeignKey behaves the same way: tag2.content_object raises Tag.DoesNotExist.
Cause: fetch_many() on ForwardManyToOneDescriptor and GenericForeignKeyDescriptor passes all peers to prefetch_related_objects(). The prefetch reads the deferred author_id of the deleted peer, and DeferredAttribute raises DoesNotExist for it.
#37436 says "The missing peer should not cause an unrelated peer's deferred-field access to fail." The same should hold for related objects.
2. A non-null foreign key returns None
book1, book2 = Book.objects.fetch_mode(FETCH_PEERS).order_by("pk") Author.objects.filter(pk=book2.author_id).delete() book2.author
FETCH_PEERS returns None, although author isn't nullable. FETCH_ONE raises Author.DoesNotExist. prefetch_related() raises RelatedObjectDoesNotExist. ForwardManyToOneDescriptor.__get__() returns right after fetch() and skips its not self.field.null check.
Possible fix
In fetch_many(), skip peers whose own row is gone (their local related value raises DoesNotExist). In __get__(), run the null check after fetching too. I have a draft patch with tests and can submit it if this is accepted.