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)
Change History (5)
by , 13 years ago
Attachment: | 17564-test.diff added |
---|
comment:1 by , 13 years ago
Keywords: | postgresql added |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | 1.3 → SVN |
The attached test passes on SQLite and MySQL, but fails with PostgreSQL.
comment:2 by , 13 years ago
Cc: | 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 , 13 years ago
Can you please explain how to do it through django without using the patch?
comment:4 by , 9 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
This can be easily done using https://docs.djangoproject.com/en/1.8/ref/models/conditional-expressions/ since 1.8.
Test showing failure