Changes between Initial Version and Version 9 of Ticket #35309
- Timestamp:
- Mar 16, 2024, 9:10:49 AM (8 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #35309
- Property Has patch set
- Property Cc added
- Property Keywords single-valued added
- Property Patch needs improvement set
- Property Triage Stage Unreviewed → Accepted
- Property Summary Remove Order by on models when prefetching by id → Elide ordering of prefetch querysets for single valued relationships
-
Ticket #35309 – Description
initial v9 1 Hello, 1 While the ordering of multi-valued relationships must be preserved when prefetching relationships is it unnecessary when using `prefetch_related` against single valued relationships. 2 2 3 I don't know if the "bug" is still here with Django 5. 4 But on my version of Django, I have the following "bug" : 5 Assume you have the following code : 3 For example, given the following models 6 4 7 5 {{{#!python 8 class A (models.Model):6 class Author(models.Model): 9 7 name = models.CharField(max_length=200) 10 8 … … 13 11 14 12 15 class B(models.Model): 16 a = models.ForeignKey(A, related_name="bs", on_delete=models.CASCADE) 13 class Book(models.Model): 14 title = models.CharField(max_length=200) 15 author = models.ForeignKey(Author, related_name="books", on_delete=models.CASCADE) 17 16 18 19 a1 = A.objects.create(name="a1") 20 a2 = A.objects.create(name="a2") 21 a3 = A.objects.create(name="a3") 22 a4 = A.objects.create(name="a4") 23 a5 = A.objects.create(name="a5") 24 a6 = A.objects.create(name="a6") 25 a7 = A.objects.create(name="a7") 26 27 b1 = B.objects.create(a=a1) 28 b2 = B.objects.create(a=a2) 29 b3 = B.objects.create(a=a3) 30 b4 = B.objects.create(a=a4) 31 b5 = B.objects.create(a=a5) 32 b6 = B.objects.create(a=a6) 33 b7 = B.objects.create(a=a7) 34 35 bs = B.objects.all().prefetch_related("a") 17 class Meta: 18 ordering = ["title"] 36 19 }}} 37 20 38 The prefetch of as will use the order by and add useless charge on the DB server. 39 There may be other cases than ForeignKey where the order by is useless. 40 But since OneToOne inherits from ForeignKey, I don't see anything else right now. 21 The ordering of an author's books in `Author.objects.prefetch_related("books")` has a significance as multiple books might be associated with each authors. 41 22 42 Hence, I request this enhancement, please :) 43 #ClimateChangeBrake 23 It's not the case for a book's author in `Book.objects.prefetch_related("author")` through as the relationship can only contain a single author and there is a single way to order the members of a singleton. 44 24 45 Best regards, 46 Laurent Lyaudet 25 In other words `sorted([element], key=sort_func)` will result in `[element]` for any `sort_func`. 47 26 48 49 50 51 27 This property holds true for all the single valued relationships that the ORM supports (backward and forward 1:1 and forward 1:M) which allows the prefetching to elide any predefined ordering safely to avoid an unnecessary and possibly expensive ordering defined for the related model queryset.