#35751: Ordering a model via a m2m field creates unintended side effect for
ForeignKeys
-------------------------------------+-------------------------------------
     Reporter:  Dennis Scheiba       |                    Owner:  (none)
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  5.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  ORM ordering         |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

 I personally think that `order_by(multivalued__value)` is a foot-gun due
 to its ambiguity and duplicate row spanning behaviour and should be
 deprecated but it poses some challenges as it is a behavior that was
 supported and even documented for over a decade so it's safe to assume
 that some users might be legitimately depend on it for some nice query
 needs (e.g. adding `distinct()` afterwards to deal with the duplicate row
 issue).

 If we were to raise warnings or consider deprecating this feature we
 should have an upgrade path that covers these use cases. In your
 particular case


 {{{#!python
 OrderItem.objects.order_by("booking_times__date").distinct()
 }}}

 which results in

 {{{#!sql
 SELECT DISTINCT orderitem.*
 FROM orderitem
 LEFT JOIN orderitem_booking_times ON (orderitem_booking_times.orderitem_id
 = orderitem.id)
 LEFT JOIN bookingtime ON (orderitem_booking_times.bookingtime_id =
 bookingtime.id)
 ORDER BY bookingtime.date
 }}}

 can be replaced with

 {{{#!python
 OrderItem.objects.order_by(Min("booking_times__date"))
 }}}

 which results in

 {{{#!sql
 SELECT orderitem.*
 FROM orderitem
 LEFT JOIN orderitem_booking_times ON (orderitem_booking_times.orderitem_id
 = orderitem.id)
 LEFT JOIN bookingtime ON (orderitem_booking_times.bookingtime_id =
 bookingtime.id)
 GROUP BY orderitem.id
 ORDER BY MIN(bookingtime.date)
 }}}

 Since `order_by` is resolved late in the query compilation process it
 should be doable to present a deprecation warning that suggests the usage
 of `Min` and `Max` instead.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35751#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/01070191e1066891-c03e0191-f7e9-4aa6-90b9-08025b39c877-000000%40eu-central-1.amazonses.com.

Reply via email to