#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018dc13c71e3-b27c4a1c-ea55-4f33-bf16-85b86e1c2f61-000000%40eu-central-1.amazonses.com.

Reply via email to