Opened 13 years ago

Closed 9 years 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: dev
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 13 years ago.
Test showing failure

Download all attachments as: .zip

Change History (5)

by Claude Paroz, 13 years ago

Attachment: 17564-test.diff added

Test showing failure

comment:1 by Claude Paroz, 13 years ago

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

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

comment:2 by Anssi Kääriäinen, 13 years ago

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

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

comment:4 by Raphael Michel, 9 years ago

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