#19195: Using distinct([*fields]) filter on a foreign key produces an ordering error when the foreign key has a Meta ordering field. -------------------------------------+------------------------------------- Reporter: chrisedgemon@… | Owner: nobody Type: Bug | Status: new Component: Database layer | Version: 1.4 (models, ORM) | Resolution: Severity: Normal | Triage Stage: Accepted Keywords: distinct, query | Needs documentation: 0 Has patch: 0 | Patch needs improvement: 0 Needs tests: 0 | UI/UX: 0 Easy pickings: 0 | -------------------------------------+------------------------------------- Changes (by charettes):
* cc: charettes (added) Old description: > I tried to using a distinct filter like this: > Appearance.objects.order_by('team').distinct('team'); this fails with the > following Database Error: "DatabaseError: SELECT DISTINCT ON expressions > must match initial ORDER BY expressions" > > It is possible to work around this problem by using this modified filter: > Appearance.objects.order_by('team__id').distinct('team__id'). > > Model definition: http://pastebin.com/index/J45fy9fr > Full traceback: http://pastebin.com/feSFMbzX New description: I tried to using a distinct filter like this: Appearance.objects.order_by('team').distinct('team'); this fails with the following Database Error: "DatabaseError: SELECT DISTINCT ON expressions must match initial ORDER BY expressions" It is possible to work around this problem by using this modified filter: `Appearance.objects.order_by('team__id').distinct('team__id')`. Model definition: http://pastebin.com/index/J45fy9fr Full traceback: http://pastebin.com/feSFMbzX -- Comment: Just hit this issue and had a hard time figuring out what I've done wrong. Intuitively I tried `.distinct('related_id').order_by('related_id')` after realizing removing my `Related._meta.ordering` solved the issue but, as pointed out by akaariai, this is not allowed ATM. Replying to [comment:3 akaariai]: > I think we should allow using .order_by('team_id') here. > > Related fields have two attributes on model level - team and team_id in this case. We allow using team_id in many places in the ORM already, and to me it seems we should allow it in order_by and distinct, too. > > Is there some reason to *not* allow them? I can't think of any reason we'd like *not* to allow them. It looks like sanest to expose an API to explicitly opt-out of the existing related model ordering behavior while maintaining backward compatibility. -- Ticket URL: <https://code.djangoproject.com/ticket/19195#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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/080.4d3073d2fcf832f595947f8125decf49%40djangoproject.com. For more options, visit https://groups.google.com/groups/opt_out.