#35309: Remove Order by on models when prefetching by id -------------------------------------+------------------------------------- Reporter: Laurent | Owner: nobody Lyaudet | Type: | Status: new Cleanup/optimization | Component: Database | Version: 5.0 layer (models, ORM) | Severity: Normal | Keywords: prefetch order_by Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- Hello,
I don't know if the "bug" is still here with Django 5. But on my version of Django, I have the following "bug" : Assume you have the following code : {{{#!python class A(models.Model): name = models.CharField(max_length=200) class Meta: ordering = ["name"] class B(models.Model): a = models.ForeignKey(A, related_name="bs", on_delete=models.CASCADE) a1 = A.objects.create(name="a1") a2 = A.objects.create(name="a2") a3 = A.objects.create(name="a3") a4 = A.objects.create(name="a4") a5 = A.objects.create(name="a5") a6 = A.objects.create(name="a6") a7 = A.objects.create(name="a7") b1 = B.objects.create(a=a1) b2 = B.objects.create(a=a2) b3 = B.objects.create(a=a3) b4 = B.objects.create(a=a4) b5 = B.objects.create(a=a5) b6 = B.objects.create(a=a6) b7 = B.objects.create(a=a7) bs = B.objects.all().prefetch_related("a") }}} The prefetch of as will use the order by and add useless charge on the DB server. There may be other cases than ForeignKey where the order by is useless. But since OneToOne inherits from ForeignKey, I don't see anything else right now. Hence, I request this enhancement, please :) #ClimateChangeBrake Best regards, Laurent Lyaudet -- Ticket URL: <https://code.djangoproject.com/ticket/35309> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107018e441bdcfa-821fa4bc-9163-4caf-bafe-cec82c9d0649-000000%40eu-central-1.amazonses.com.