[Django] #35235: ArrayAgg inconsistent return value when no results

15 views
Skip to first unread message

Django

unread,
Feb 19, 2024, 6:59:45 AM2/19/24
to django-...@googlegroups.com
#35235: ArrayAgg inconsistent return value when no results
--------------------------------------------+--------------------------
Reporter: Per Carlsen | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 5.0
Severity: Normal | Keywords: ArrayAgg
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+--------------------------
ArrayAgg changed the default return value in Django 5.0 to `None` instead
of `[]`. The documentation recommends adding `default=Value([])` to make
it return an empty list instead of `None`. My app requires empty lists to
be returned, so I updated the code and noticed that the return type is
inconsistent with the documentation when using the `filter` functionality
of ArrayAgg.

Consider the following query. `filter_value` and `default` are variables,
and given combinations seem to give inconsistent return values.

{{{
MyModel.objects.annotate(
annotated_ids=ArrayAgg(
"my_other_model_set__id",
filter=Q(
my_other_model_set__id__in=filter_value,
),
default=default,
)
).first().annotated_ids
}}}


`MyOtherModel` has a fk relation to `MyModel`, which we referenced via
`my_other_model_set`. Consider the following input values for
`filter_value` and `default`:

Example 1:
filter_value = [-1] # Or any other ID that does not exist
default = Value([])
This returns an empty list (`[]`), as expected because there are no
results for ArrayAgg

Example 2:
filter_value = []
default = Value([])
BUG: This returns the string `'{}'` instead of an empty list. This also
happens if we don't specify a `default` value. But if we give `default=[]`
(without the `Value()`), the return value is consistent with the
documentation.
--
Ticket URL: <https://code.djangoproject.com/ticket/35235>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 20, 2024, 3:15:25 AM2/20/24
to django-...@googlegroups.com
#35235: ArrayAgg inconsistent return value when no results
----------------------------------+--------------------------------------
Reporter: Per Carlsen | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 5.0
Severity: Normal | Resolution:
Keywords: ArrayAgg | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Comment (by Sharon Woo):

New to django core, I couldn't replicate this issue with some simple tests
in a draft PR: https://github.com/django/django/pull/17890
--
Ticket URL: <https://code.djangoproject.com/ticket/35235#comment:1>

Django

unread,
Feb 20, 2024, 3:19:40 AM2/20/24
to django-...@googlegroups.com
#35235: ArrayAgg inconsistent return value when no results
----------------------------------+--------------------------------------
Reporter: Per Carlsen | Owner: Sharon Woo
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 5.0
Severity: Normal | Resolution:
Keywords: ArrayAgg | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Sharon Woo):

* owner: (none) => Sharon Woo
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/35235#comment:2>
Reply all
Reply to author
Forward
0 new messages