Opened 3 years ago

Last modified 5 months ago

#17564 new Bug

django.db.models.Sum does not work on BooleanField

Reported by: dp_wiz Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: postgresql
Cc: anssi.kaariainen@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Models are Point -(fkey)-> Group and i'm trying to annotate Group queryset with a number of total points and a number of "active" points. Point.active is a BooleanField.

Code:

queryset = Group.objects.annotate(
    point_count = Count('point'),
    points_active = Sum('point__active'))

Error:

Exception Type: DatabaseError at /kiosks/wl/
Exception Value: function sum(boolean) does not exist
LINE 1: ..."id", "kiwl_group"."title", "kiwl_group"."descr", SUM("kiwl_...
                                                             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

However, there is a Bug Report which is marked "fixed" and it looks like there is something i could to to actually get a Sum on BoolanField-s.

Attachments (1)

17564-test.diff (3.0 KB) - added by claudep 3 years ago.
Test showing failure

Download all attachments as: .zip

Change History (4)

Changed 3 years ago by claudep

Test showing failure

comment:1 Changed 3 years ago by claudep

  • Keywords postgresql added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.3 to SVN

The attached test passes on SQLite and MySQL, but fails with PostgreSQL.

comment:2 Changed 3 years ago by akaariai

  • Cc anssi.kaariainen@… added

The problem here is that PostgreSQL would need a type cast to int. So, the SQL should look like SUM("some_boolean_field"::int) for the SUM to work. However I am not sure if doing this in Django is a good idea, mainly because this would complicate the code somewhat, and something like #11305 would solve the issue for the user by allowing generation of SQL like in the Mozilla bug report.

I am leaving this ticket still open, although I would like to close this as wontfix for the reasons mentioned above. Could this be marked duplicate (or "related to") to #11305?

comment:3 Changed 3 years ago by anonymous

Can you please explain how to do it through django without using the patch?

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