[Django] #33594: order_by("field1__-field2") yields inconsistent behavior

2 views
Skip to first unread message

Django

unread,
Mar 22, 2022, 7:40:06 AM3/22/22
to django-...@googlegroups.com
#33594: order_by("field1__-field2") yields inconsistent behavior
-------------------------------------+-------------------------------------
Reporter: Thomas | Owner: nobody
Chaumeny |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
In our codebase, someone used `SomeModel.objects.order_by("foo__-
created_at")` to order according to a foreign key's field and it was
working (yielding the correct query with `DESC` order).

However, after we added the following index on `SomeModel`:

{{{
class Meta:
indexes = [
models.Index(fields=["some_other_field", "created_at"]),
]
}}}

The same code `SomeModel.objects.order_by("foo__-created_at")` stopped
working and started raising an error:

{{{
django.core.exceptions.FieldError: Cannot resolve keyword '-created_at'
into field. Choices are: context, created_at, id, tag_readings,
updated_at, user_id, warehouse_id
}}}

Shouldn't the pattern for ordering in a descending order with chaining be
documented?

According to
https://github.com/django/django/blob/main/django/db/models/sql/compiler.py#L402-L403
it should be `-foo__created_at`.
Also, we should expect the wrong pattern to fail unconditionally.

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

Django

unread,
Mar 22, 2022, 7:51:21 AM3/22/22
to django-...@googlegroups.com
#33594: order_by("field1__-field2") yields inconsistent behavior
-------------------------------------+-------------------------------------
Reporter: Thomas Chaumeny | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Thomas Chaumeny:

Old description:

> In our codebase, someone used `SomeModel.objects.order_by("foo__-
> created_at")` to order according to a foreign key's field and it was
> working (yielding the correct query with `DESC` order).
>
> However, after we added the following index on `SomeModel`:
>
> {{{
> class Meta:
> indexes = [
> models.Index(fields=["some_other_field", "created_at"]),
> ]
> }}}
>
> The same code `SomeModel.objects.order_by("foo__-created_at")` stopped
> working and started raising an error:
>
> {{{
> django.core.exceptions.FieldError: Cannot resolve keyword '-created_at'
> into field. Choices are: context, created_at, id, tag_readings,
> updated_at, user_id, warehouse_id
> }}}
>
> Shouldn't the pattern for ordering in a descending order with chaining be
> documented?
>
> According to
> https://github.com/django/django/blob/main/django/db/models/sql/compiler.py#L402-L403
> it should be `-foo__created_at`.
> Also, we should expect the wrong pattern to fail unconditionally.

New description:

In our codebase, someone used `SomeModel.objects.order_by("foo__-
created_at")` to order according to a foreign key's field and it was
working (yielding the correct query with `DESC` order).

However, after we added the following index on `SomeModel`:

{{{
class Meta:
indexes = [
models.Index(fields=["some_other_field", "-created_at"]),
]
}}}

The same code `SomeModel.objects.order_by("foo__-created_at")` stopped
working and started raising an error:

{{{
django.core.exceptions.FieldError: Cannot resolve keyword '-created_at'
into field. Choices are: context, created_at, id, tag_readings,
updated_at, user_id, warehouse_id
}}}

Shouldn't the pattern for ordering in a descending order with chaining be
documented?

According to
https://github.com/django/django/blob/main/django/db/models/sql/compiler.py#L402-L403
it should be `-foo__created_at`.
Also, we should expect the wrong pattern to fail unconditionally.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/33594#comment:1>

Django

unread,
Mar 22, 2022, 8:30:56 AM3/22/22
to django-...@googlegroups.com
#33594: order_by("field1__-field2") yields inconsistent behavior
-------------------------------------+-------------------------------------
Reporter: Thomas Chaumeny | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

`"foo__-created_at"` it's not intended, documented, or officially
supported. It works by coincidence. I also cannot reproduce `FieldError`
after adding an index in `Meta.indexes`, I'm nor sure how adding an index
can interrupt ordering 🤔. There is something wrong in the described
models. Once `created_by` is a field on `SomeModel` and once on `foo`.
Can you double-check and provide a reproducible scenario?

--
Ticket URL: <https://code.djangoproject.com/ticket/33594#comment:2>

Django

unread,
Mar 22, 2022, 8:45:55 AM3/22/22
to django-...@googlegroups.com
#33594: order_by("field1__-field2") yields inconsistent behavior
-------------------------------------+-------------------------------------
Reporter: Thomas Chaumeny | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Thomas Chaumeny:

Old description:

> In our codebase, someone used `SomeModel.objects.order_by("foo__-
> created_at")` to order according to a foreign key's field and it was
> working (yielding the correct query with `DESC` order).
>
> However, after we added the following index on `SomeModel`:
>
> {{{
> class Meta:
> indexes = [

> models.Index(fields=["some_other_field", "-created_at"]),
> ]
> }}}
>
> The same code `SomeModel.objects.order_by("foo__-created_at")` stopped
> working and started raising an error:
>
> {{{
> django.core.exceptions.FieldError: Cannot resolve keyword '-created_at'
> into field. Choices are: context, created_at, id, tag_readings,
> updated_at, user_id, warehouse_id
> }}}
>
> Shouldn't the pattern for ordering in a descending order with chaining be
> documented?
>
> According to
> https://github.com/django/django/blob/main/django/db/models/sql/compiler.py#L402-L403
> it should be `-foo__created_at`.
> Also, we should expect the wrong pattern to fail unconditionally.

New description:

In our codebase, someone used `SomeModel.objects.order_by("foo__-
created_at")` to order according to a foreign key's field and it was
working (yielding the correct query with `DESC` order).

However, after we added the following index on `Foo`:

{{{
class Meta:
indexes = [
models.Index(fields=["some_other_field", "-created_at"]),
]
}}}

The same code `SomeModel.objects.order_by("foo__-created_at")` stopped
working and started raising an error:

{{{
django.core.exceptions.FieldError: Cannot resolve keyword '-created_at'
into field. Choices are: context, created_at, id, tag_readings,
updated_at, user_id, warehouse_id
}}}

Shouldn't the pattern for ordering in a descending order with chaining be
documented?

According to
https://github.com/django/django/blob/main/django/db/models/sql/compiler.py#L402-L403
it should be `-foo__created_at`.
Also, we should expect the wrong pattern to fail unconditionally.

--

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

Django

unread,
Mar 22, 2022, 2:23:56 PM3/22/22
to django-...@googlegroups.com
#33594: order_by("field1__-field2") yields inconsistent behavior
-------------------------------------+-------------------------------------
Reporter: Thomas Chaumeny | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => needsinfo


Comment:

Thanks for updates, however I cannot reproduce `FieldError` after adding
an index in `Meta.indexes` for `Foo`. Please provide a sample project that
reproduces this issue. Can you reproduce on Django 4.0? or the current
`main` branch?

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

Reply all
Reply to author
Forward
0 new messages