#33114: StringAgg always fails on TextField
-------------------------------------+-------------------------------------
               Reporter:  Swen       |          Owner:  nobody
  Kooij                              |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  3.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Example:

 {{{#!python
 class MyModel(models.Model):
      name = models.TextField()

 list(MyModel.objects.annotate(names=StringAgg('name', ',')))
 }}}

 Results in the error:

 {{{
 django.core.exceptions.FieldError: Expression contains mixed types:
 TextField, CharField. You must set output_field.
 }}}

 There are two possible work-arounds:

 1. Derive from `StringAgg` and set the `output_field`:

        {{{#!python
        class TextStringAgg(StringAgg):
            output_field = TextField()
        }}}

 2. Wrap the expression in `ExpressionWrapper`:

        {{{#!python
        ExpressionWrapper(StringAgg('name', ','), output_field=TextField())
        }}}

 This seems to be a regression introduced in 3.2 as part of this change:
 
https://github.com/django/django/commit/1e38f1191de21b6e96736f58df57dfb851a28c1f

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33114>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.82438dd611ca6a3495df78109d9da31a%40djangoproject.com.

Reply via email to