#32398: Excluding on annotations doesn't apply null handling.
------------------------------------------+------------------------
               Reporter:  Gordon Wrigley  |          Owner:  nobody
                   Type:  Uncategorized   |         Status:  new
              Component:  Uncategorized   |        Version:  3.1
               Severity:  Normal          |       Keywords:
           Triage Stage:  Unreviewed      |      Has patch:  0
    Needs documentation:  0               |    Needs tests:  0
Patch needs improvement:  0               |  Easy pickings:  0
                  UI/UX:  0               |
------------------------------------------+------------------------
 I would generally expect `my_timestamp__year` to behave the same as
 `.annotate(my_timestamp_year=ExtractYear("my_timestamp"))` however I have
 noticed that when using exclude the former will account for null's but the
 later won't.

 {{{
 
print(M.objects.exclude(closed_at__year=t.year).values("closed_at__year").query)

 SELECT
     EXTRACT('year' FROM "r_m"."closed_at" AT TIME ZONE 'Europe/London')
 FROM "r_m"
 WHERE NOT (
     "r_m"."closed_at" BETWEEN 2021-01-01 00:00:00+00:00 AND 2021-12-31
 23:59:59.999999+00:00
     AND "r_m"."closed_at" IS NOT NULL
 )
 }}}

 {{{
 
print(M.objects.annotate(year=ExtractYear("closed_at")).exclude(year=t.year).values("year").query)

 SELECT
      EXTRACT('year' FROM "r_m"."closed_at" AT TIME ZONE 'Europe/London')
 AS "year"
 FROM "r_m"
 WHERE NOT (
     "r_m"."closed_at" BETWEEN 2021-01-01 00:00:00+00:00 AND 2021-12-31
 23:59:59.999999+00:00
 )
 }}}

 Annotated aggregates like min and max also don't account for nulls which
 leads me to suspect this is a problem with annotations.

 I also tried wrapping the ExtractYear in an ExpressionWrapper with
 `output_field=IntegerField(null=True)` which didn't change anything.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32398>
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/050.5e8f35b975b4748ac2842e7a5460f083%40djangoproject.com.

Reply via email to