﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35309	Remove Order by on models when prefetching by id	Laurent Lyaudet	nobody	"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





"	Cleanup/optimization	new	Database layer (models, ORM)	5.0	Normal		prefetch order_by	Laurent Lyaudet	Unreviewed	0	0	0	0	0	0
