#34533: OuterRef not resolved as part oh ORDER BY clause
-------------------------------------+-------------------------------------
Reporter: REGNIER Guillaume | Owner: Shafiya
| Adzhani
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: OuterRef, OrderBy | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Shafiya Adzhani):
Replying to [comment:10 Simon Charette]:
> The hard part is here is likely due to two factor
>
> 1. `OuterRef` resolving is quite finicky and depends on a specific chain
of resolving during query composition (subquery is resolved and then the
outer query is)
> 2. `order_by` members are unfortunately not resolved at query
composition time (when `QuerySet.order_by` is called) but
[https://github.com/django/django/blob/12ffcfc350a19bbfbc203126a9b6c84b5e0d0ba2/django/db/models/sql/compiler.py#L466
at query compilation time].
>
> The reason for 2. is that `QuerySet.order_by` calls are not additive but
destructive and that the query composition logic doesn't support a generic
way of eliding joins that we previously created for some operations.
>
> For example, say that you have a query that does `qs =
Book.objects.order_by("author__name")`. If the order by clause is
immediately resolved then a `JOIN` to `author` must be created. If the
same query has then its ordering changed to `qs.order_by("title")` then
we'd want to clear the join to `author` as it's no longer necessary.
>
> In order to avoid implementing the logic to ''unreference'' relations
the `order_by` method deferred the resolving to the very end of query
compilation which is a different path than the ''normal'' resolving taking
place in additive methods such as `annotate` and `filter`.
>
> I suspect this ticket will be hard to solve without tackling the large
problem of compile time resolving of `order_by`.
Thank you for the pointer! Since this ticket is complicated, I'll leave it
to someone who is interested in solving this problem.
--
Ticket URL: <https://code.djangoproject.com/ticket/34533#comment:11>
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018d443ecffa-c80bf94c-2a7c-4a24-ae70-c01f97da41c0-000000%40eu-central-1.amazonses.com.