#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.

-- 
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/0107017fb16d44d4-2e89fc21-9c70-4589-bdaa-60183225cdf7-000000%40eu-central-1.amazonses.com.

Reply via email to