Opened 5 years ago

Closed 13 months ago

#17564 closed Bug (fixed)

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

Reported by: 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 Claude Paroz 5 years ago.
Test showing failure

Download all attachments as: .zip

Change History (5)

Changed 5 years ago by Claude Paroz

Attachment: 17564-test.diff added

Test showing failure

comment:1 Changed 5 years ago by Claude Paroz

Keywords: postgresql added
Triage Stage: UnreviewedAccepted
Version: 1.3SVN

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

comment:2 Changed 5 years ago by Anssi Kääriäinen

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 5 years ago by anonymous

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

comment:4 Changed 13 months ago by Raphael Michel

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top