id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux 32200 Aggregating when grouping on an ExpressionWrapper omits the expression from the group by Gordon Wrigley Hasan Ramezani "I ran into this with Postgres on Django 3.1.3, I'm not sure what other versions it exists on. {{{ #!python print( Fred.objects.annotate( bob_id__is_null=ExpressionWrapper( Q(bob_id=None), output_field=BooleanField() ) ).values( ""bob_id__is_null"" ).annotate( id__count=Count(""id"", distinct=True) ).values( ""bob_id__is_null"", ""id__count"" ).query ) }}} {{{ #!sql SELECT ""main_fred"".""bob_id"" IS NULL AS ""bob_id__is_null"", COUNT(DISTINCT ""main_fred"".""id"") AS ""id__count"" FROM ""main_fred"" GROUP BY ""main_fred"".""bob_id"" }}} On the last line there the group by has dropped the ""IS NULL""" Bug closed Database layer (models, ORM) 3.1 Release blocker fixed gordon.wrigley@… Simon Charette Thodoris Sotiropoulos Ready for checkin 1 0 0 0 0 0