Docs and implementation don't match regading return values of certain QuerySet aggregate functions
|Reported by:||PaulM||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The aggregation docs specify that Max, Min, or Sum return a value of the same type as the field on which the function was called. This means that if I use Max() on a PositiveIntegerField, I expect to get back a Python int.
I know that a non-null PositiveIntegerField can't contain None - setting and saving it throws an IntegrityError.
However, when I use the Max() aggregate on a PositiveIntegerField in a model with no rows, I get back None. This behavior occurs in both sqlite and PostgreSQL.
Since this behavior has (presumably) been around since aggregates were introduced, this must be a bug in the docs, and not in Django, even though it is counterintuitive and requires boilerplate code to make absolutely certain that the value is not None before depending on the typing.
If it were up to me, I would propose changing the return value to be in line with the docs, but that wouldn't be backwards compatible.
Change History (10)
comment:1 Changed 6 years ago by Alex
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:7 Changed 5 years ago by ramiro
- Summary changed from Aggregates docs are misleading (or there is a bug in the aggregate code) to Docs and implementation don't match regading return values of certain QuerySet aggregate functions
comment:9 Changed 5 years ago by ramiro
- Easy pickings unset
- Resolution set to duplicate
- Status changed from new to closed