#33682: SQL generation bug in `.distinct()` when supplied fields go through
multiple many-related tables
-------------------------------------+-------------------------------------
     Reporter:  Robert Leach         |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  sql, distinct,       |             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):

 Replying to [comment:5 Robert Leach]:
 > When those methods are assessed individually, I understand why those
 fields are the preferred solution (e.g. the meta ordering may not be
 unique), but given that `distinct` requires the same fields be present at
 the beginning of the order-by, I don't know what prevents the code to be
 written to have those fields be resolved in a way that is copacetic.
 Like, why not convert the reference into 2 additional fields that
 together, meet both requirements (`name` AND `compound_id`)? Order-by
 would be satisfied and distinct would be satisfied.  Or... in my case,
 `name` is unique, so distinct could resolve to the meta ordering without
 issue...
 >
 > Is there a technical reason the code doesn't already do this?

 This would be another logic that's implicit and probably unexpected by
 users (at least in some cases). As far as I'm aware it's preferable to
 fail loudly even with a `ProgrammingError`. I'm not sure how to improve
 this note, maybe it's enough to add a correct example:

 {{{#!diff
 diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
 index a9da1dcf7e..891b8255b0 100644
 --- a/docs/ref/models/querysets.txt
 +++ b/docs/ref/models/querysets.txt
 @@ -565,7 +565,9 @@ Examples (those after the first will only work on
 PostgreSQL)::
      ...wouldn't work because the query would be ordered by ``blog__name``
 thus
      mismatching the ``DISTINCT ON`` expression. You'd have to explicitly
 order
      by the relation ``_id`` field (``blog_id`` in this case) or the
 referenced
 -    one (``blog__pk``) to make sure both expressions match.
 +    one (``blog__pk``) to make sure both expressions match::
 +
 +        Entry.objects.order_by('blog_id').distinct('blog_id')

  ``values()``
  ~~~~~~~~~~~~
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:6>
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/01070180ac6ab9dc-f364073d-6c35-48f3-a491-0f1f38ab81f1-000000%40eu-central-1.amazonses.com.

Reply via email to