Re: [Django] #34012: QuerySet.order_by() silently skips non-existing fields on related fields.

37 views
Skip to first unread message

Django

unread,
Sep 16, 2022, 10:11:53 AM9/16/22
to django-...@googlegroups.com
#34012: QuerySet.order_by() silently skips non-existing fields on related fields.
-------------------------------------+-------------------------------------
Reporter: Klaas van Schelven | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

* cc: David Sanders (added)


Comment:

fwiw I can't reproduce the issue on 4.1.1 🤔 …

Here's what I tried (full tracebacks omitted for brevity):

{{{
class Parent(Model):
...

class Child(Model):
parent = ForeignKey(Parent, on_delete=CASCADE)


Child.objects.all().order_by('asdf')
FieldError: Cannot resolve keyword 'asdf' into field. Choices are: id,
parent, parent_id

Child.objects.all().order_by('parent__id')
<QuerySet []>

Child.objects.all().order_by('parent__asdf')
FieldError: Unsupported lookup 'asdf' for BigAutoField or join on the
field not permitted, perhaps you meant df?

During handling of the above exception, another exception occurred:
FieldError: Cannot resolve keyword 'asdf' into field. Choices are: child,
id
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34012#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 17, 2022, 3:22:38 AM9/17/22
to django-...@googlegroups.com
#34012: QuerySet.order_by() silently skips non-existing fields on related fields.
-------------------------------------+-------------------------------------
Reporter: Klaas van Schelven | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Klaas van Schelven):

Good catch, this only happens when an explicit ordering is defined on the
`Parent`'s `Meta`:

{{{
from django.db import models


class Parent(models.Model):
class Meta:
ordering = ["id"]


class Child(models.Model):
parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
}}}

Reproducable up to Django 4.1.1

--
Ticket URL: <https://code.djangoproject.com/ticket/34012#comment:3>

Django

unread,
Sep 17, 2022, 6:28:29 AM9/17/22
to django-...@googlegroups.com
#34012: QuerySet.order_by() silently skips non-existing fields on related fields.
-------------------------------------+-------------------------------------
Reporter: Klaas van Schelven | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by David Sanders):

felixx this looks like an issue with overriding the foreign model's
ordering.

If I repeat the test with Klaas' models and print the query it reveals it
still attempts to use `parent.id`:


{{{
class Parent(Model):


class Meta:
ordering = ["id"]

class Child(Model):
parent = ForeignKey(Parent, on_delete=CASCADE)

print(Child.objects.all().order_by('parent__asdf').query)
SELECT "sample_child"."id", "sample_child"."parent_id" FROM "sample_child"
INNER JOIN "sample_parent" ON ("sample_child"."parent_id" =
"sample_parent"."id") ORDER BY "sample_parent"."id" ASC
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34012#comment:4>

Django

unread,
Sep 17, 2022, 12:13:52 PM9/17/22
to django-...@googlegroups.com
#34012: QuerySet.order_by() silently skips non-existing fields on related fields.
-------------------------------------+-------------------------------------
Reporter: Klaas van Schelven | Owner: David
Type: | Sanders
Cleanup/optimization | Status: assigned

Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

* owner: nobody => David Sanders
* status: new => assigned
* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/16070

--
Ticket URL: <https://code.djangoproject.com/ticket/34012#comment:5>

Django

unread,
Sep 18, 2022, 2:08:14 AM9/18/22
to django-...@googlegroups.com
#34012: QuerySet.order_by() silently skips non-existing fields on related fields.
-------------------------------------+-------------------------------------
Reporter: Klaas van Schelven | Owner: David
Type: | Sanders
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.1

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

* version: 4.0 => 4.1


--
Ticket URL: <https://code.djangoproject.com/ticket/34012#comment:6>

Reply all
Reply to author
Forward
0 new messages