#31150: SELECT DISTINCT is not not so distict
-------------------------------------+-------------------------------------
     Reporter:  Johannes Hoppe       |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.0
  (models, ORM)                      |
     Severity:  Release blocker      |               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 Johannes Hoppe):

 * status:  closed => new
 * resolution:  needsinfo =>


Comment:

 @felixxm, it seems that `Subquery` annotation are omitted from the
 grouping section in 3.0. I will try to create a test case today.

 If you add the following test to 2.2.* anywhere in `tests.aggregation` it
 will pass, in 3.0 it will fail, because the subquery is missing from the
 grouping bit:

 {{{
     def test_filtered_aggregate_ref_subquery_annotation_e_31150(self):
         aggs = Author.objects.annotate(
             earliest_book_year=Subquery(
                 Book.objects.filter(
                     contact__pk=OuterRef('pk'),
                 ).order_by('pubdate').values('pubdate__year')[:1]
             ),
         ).annotate(max_id=Max('id'))
         print(aggs.query)
         self.assertIn(str(aggs.query), """
         SELECT "aggregation_author"."id", "aggregation_author"."name",
 "aggregation_author"."age", (SELECT django_date_extract('year',
 U0."pubdate") FROM "aggregation_book" U0 WHERE U0."contact_id" =
 ("aggregation_author"."id") ORDER BY U0."pubdate" ASC  LIMIT 1) AS
 "earliest_book_year", MAX("aggregation_author"."id") AS "max_id" FROM
 "aggregation_author" GROUP BY "aggregation_author"."id",
 "aggregation_author"."name", "aggregation_author"."age", (SELECT
 django_date_extract('year', U0."pubdate") FROM "aggregation_book" U0 WHERE
 U0."contact_id" = ("aggregation_author"."id") ORDER BY U0."pubdate" ASC
 LIMIT 1)
         """)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31150#comment:4>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.3a7e5eac8650cf9d78bcb9f988105dd9%40djangoproject.com.

Reply via email to