Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32463 closed Bug (duplicate)

'Sum' aggregate function not working with Window function on SQLite.

Reported by: blackhorsesacher Owned by: nobody
Component: Database layer (models, ORM) Version: 3.1
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 (last modified by Mariusz Felisiak)

'Sum' aggregate function not working with Window function for sqlite3 version 3.27.2
Produces these errors:

>>>sqlite3.OperationalError: near "OVER": syntax error
>>>django.db.utils.OperationalError: near "OVER": syntax error

This is similar to the fixed issue #30027 "SQLite (pre 3.25.0) does not support window functions, raises OperationalError", but I am using SQLite 3.27.2 and the native SQL query is working from sqlite3 command line and also when using Model.objects.raw(), which I'm having to use as a workaround.
It should be reproducible as follows, assuming model "myapp.mymodel" has a field "amount":

>>> from django.db.models import Sum, Window
>>> from myapp.models import mymodel
>>> mymodel.objects.all().annotate(cumsum=Window(expression=Sum('amount')))

The raw query works OK:

>>> rawqs=mymodel.objects.raw("SELECT *, SUM (amount) OVER (ORDER BY id) AS cumsum FROM myapp_mymodel")

Change History (3)

comment:1 by Mariusz Felisiak, 4 years ago

Component: UncategorizedDatabase layer (models, ORM)
Description: modified (diff)
Resolution: duplicate
Status: newclosed
Summary: 'Sum' aggregate function not working with Window function for sqlite3 version 3.27.2'Sum' aggregate function not working with Window function on SQLite.

Thanks for this report, It looks like a duplicate of #31723 (fixed in 71d10ca8c90ccc1fd0ccd6683716dd3c3116ae6a, Django 3.2+) . Can you confirm that amount is a DecimalField?

comment:2 by blackhorsesacher, 4 years ago

Thank you for the very rapid response. Yes, I confirm that 'amount' is a DecimalField

in reply to:  2 comment:3 by blackhorsesacher, 4 years ago

Replying to blackhorsesacher:

Thank you for the very rapid response. Yes, I confirm that 'amount' is a DecimalField

I've now tested with Django 3.2 beta 1 and confirm that it's now working well. Thanks again.

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