#24642: If queryset has orderby before distinct query selects the orderby
-------------------------------------+-------------------------------------
Reporter: ark4n631 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(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
-------------------------------------+-------------------------------------
Changes (by timgraham):
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> If you generate a query set like:
> [[BR]]
> orders = Order.objects.filter( date = paramDate, objfilter =
> paramObjfilter ).order_by( 'date' )
> dudes = orders.values('dude').distinct().exclude(dude__isnull = True)
> [[BR]]
> [[BR]]
>
> Query will be:
> [[BR]]
> SELECT DISTINCT "base_ordentrabajo"."dude_id", "base_orders"."date" FROM
> "base_orders" WHERE ...
> [[BR]]
> If you remove order_by and make distinct after query is quite different
> (and like should be)
> [[BR]]
> orders = Order.objects.filter( date = paramDate, objfilter =
> paramObjfilter )
> dudes = orders.values('dude').distinct().exclude(dude__isnull = True)
> [[BR]]
> SELECT DISTINCT "base_ordentrabajo"."dude_id" FROM "base_orders" WHERE
> ...
> [[BR]]
> So i can't make an order_by if i need to make a distinct first. Anyway
> django should warn of query.order_by or don't allow to make a subset
> query filter with distinct if you order in the first place because this
> lead to unexpected results on query (query will contain not distinct
> values for the field you are searching for because it adds the order_by
> field value to the query)
New description:
If you generate a query set like:
{{{
orders = Order.objects.filter( date = paramDate, objfilter =
paramObjfilter ).order_by( 'date' )
dudes = orders.values('dude').distinct().exclude(dude__isnull = True)
}}}
Query will be:
{{{
SELECT DISTINCT "base_ordentrabajo"."dude_id", "base_orders"."date" FROM
"base_orders" WHERE ...
}}}
If you remove order_by and make distinct after query is quite different
(and like should be)
{{{
orders = Order.objects.filter( date = paramDate, objfilter =
paramObjfilter )
dudes = orders.values('dude').distinct().exclude(dude__isnull = True)
SELECT DISTINCT "base_ordentrabajo"."dude_id" FROM "base_orders" WHERE ...
}}}
So i can't make an order_by if i need to make a distinct first. Anyway
django should warn of query.order_by or don't allow to make a subset query
filter with distinct if you order in the first place because this lead to
unexpected results on query (query will contain not distinct values for
the field you are searching for because it adds the order_by field value
to the query)
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24642#comment:1>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.5709123dabc59a1e1f2af2c749689b50%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.