Opened 5 years ago

Closed 5 years ago

#30683 closed Bug (duplicate)

QuerySet.count() crashes when window functions and conditional expressions are both used.

Reported by: Adam Sołtysik Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Any count() query with window functions inside conditional expressions, like:

Model.objects.annotate(rank=Case(When(condition=~Q(pk=None), then=Window(Rank(), order_by=F('pk'))))).count()

throws an error:

django.db.utils.ProgrammingError: window functions are not allowed in GROUP BY

The query works after adding .values('pk') to it or when conditional expressions are not used.

There is a similar inconsistency when we call e.g. .filter(rank__gt=0) instead of .count(). It doesn't work anyway (#28333), but there are two different errors depending on whether conditionals are used: django.db.utils.ProgrammingError: window functions are not allowed in WHERE or django.db.utils.NotSupportedError: Window is disallowed in the filter clause..

Change History (2)

comment:1 by Mariusz Felisiak, 5 years ago

Summary: QuerySet.count() crashes when window functions and conditional expressions are both usedQuerySet.count() crashes when window functions and conditional expressions are both used.
Version: 2.2master

QuerySet.filter() part of this ticket was fixed in #30668.

comment:2 by Mariusz Felisiak, 5 years ago

Resolution: duplicate
Status: newclosed

GROUP BY issue was fixed in 3f32154f40a855afa063095e3d091ce6be21f2c5.

Note: See TracTickets for help on using tickets.
Back to Top