Opened 5 years ago

Closed 5 years ago

#30104 closed Uncategorized (invalid)

Need filtering by Window expression

Reported by: Farid Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Normal Keywords: orm, window, row_number, filter
Cc: Farid Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I use Row_Number function with WIndow expression. And i need to filter only second rows.

History.objects.annotate(row=Window(expression=RowNumber(), partition_by=[F('ad_id')], order_by=F('updated_at').desc())).filter(row=2)

But this exception raised:

django.db.utils.NotSupportedError: Window is disallowed in the filter clause.

Why Window is disallowed in the filter clause? Is there other way to do this?

Change History (2)

comment:1 by Farid, 5 years ago

Cc: Farid added

comment:2 by Carlton Gibson, 5 years ago

Resolution: invalid
Status: newclosed

See the docs for `Window.filterable`:

The SQL standard disallows referencing window functions in the WHERE clause and Django raises an exception when constructing a QuerySet that would do that.

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