#29095: count on queryset with sum window function in django 2.0 + postgres 
throws
error
-------------------------------------+-------------------------------------
               Reporter:  Shadi      |          Owner:  nobody
  Akiki                              |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  2.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  window
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Using `.count()` on the queryset from `Sum` in
 [https://docs.djangoproject.com/en/2.0/ref/models/expressions/#window-
 functions Django 2.0 Window] function yields the error


 {{{
     django.db.utils.ProgrammingError: column
 "cms2_balances_base_v1.initial_balance" must appear in the GROUP BY clause
 or be used in an aggregate function
     LINE 1: ...T "cms2_balances_base_v1"."pub_date" AS Col1,
 SUM("cms2_bala...
 }}}

 Here is the exact django code

 {{{
     from apps.cms2_balances.models import *
     from django.db.models import Avg, F, Max, Min, Window, Sum, Func

     t1 = obligor.basev1_set.annotate(
         cumulative_obligor=Window(
             expression=Sum(F('initial_balance')),
             partition_by=[F('currency'), F('obligor')],
             order_by=['pub_date', 'content_type_id', 'object_id']
         )
     )
     t1.all() # <<< no error
     t1.count() # <<< yields error
 }}}

 The query of this queryset works when I run it in postgres directly. Here
 it is


 {{{
     SELECT
         "cms2_balances_base_v1"."obligor_id",
         "cms2_balances_base_v1"."originator_id",
         "cms2_balances_base_v1"."currency_id",
         "cms2_balances_base_v1"."content_type_id",
         "cms2_balances_base_v1"."object_id",
         "cms2_balances_base_v1"."pub_date",
         "cms2_balances_base_v1"."initial_balance",
         "cms2_balances_base_v1"."principal_balance",
         "cms2_balances_base_v1"."interest_balance",
         SUM("cms2_balances_base_v1"."initial_balance")
             OVER (
                 PARTITION BY "cms2_balances_base_v1"."currency_id",
                     "cms2_balances_base_v1"."obligor_id"
                 ORDER BY "cms2_balances_base_v1"."pub_date",
                     "cms2_balances_base_v1"."content_type_id",
                 "cms2_balances_base_v1"."object_id"
             ) AS "cumulative_obligor"
     FROM "cms2_balances_base_v1"
     WHERE "cms2_balances_base_v1"."obligor_id" = 2164
     ;
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29095>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/057.e491beee582253d37cbd1d5552cc75f2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to