#34858: Bug in output field handling
-------------------------------+--------------------------------------
     Reporter:  Toan Vuong     |                    Owner:  nobody
         Type:  Uncategorized  |                   Status:  new
    Component:  Uncategorized  |                  Version:  4.2
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------
Description changed by Toan Vuong:

Old description:

> Tested on Django 4.2.4.
>
> My model is defined like this:
>
> ```
> class Choice(models.Model):
>     choice_text = models.CharField(max_length=200)
>     votes = models.IntegerField(default=0)
>     arbitrary_num = models.PositiveIntegerField(default=1)
>     arbitrary_num2 = models.PositiveIntegerField(default=2)
> ```
>
> And this query is failing with Oracle (Tested on 19.3), but not Postgres:
>
> ```
> s = Coalesce(F('arbitrary_num') + F('arbitrary_num2'),
>                  Value(0, PositiveIntegerField()))
> case = Case(
>         When(arbitrary_num=1, then=Value(1, PositiveIntegerField())),
>         default=s,
>         output_field=PositiveIntegerField()
> )
>
> qs = Choice.objects.annotate(ss=case)
> list(qs)
>
> #qs = Choice.objects.annotate(s=s)
> #list(qs)
>
> ```
>
> The error (It's talking about the `Coalesce` function not having an
> `output_field`):
>
> ```
> django.core.exceptions.FieldError: Expression contains mixed types:
> IntegerField, PositiveIntegerField. You must set output_field.
> ```
>
> I believe the error is correct, because I think adding two
> `PositiveIntegerField()` seem to result in an `IntegerField`, so we
> *need* to specify an `output_field` on `Coalesce` to tell Django the
> proper output type to use. My question is that this seem to only throw an
> error in Oracle, and *not* Postgres. So it seems like the behavior on
> Postgres is unexpected? Somehow, the `Case` statement swallows the
> exception in Postgres and generates correct result. This can be
> demonstrated by the queryset where the `Coalesce` function is annotated
> directly without a case statement -- in this case, both Postgres and
> Oracle fails with the above error.
>
> Note that this used to work prior to 4.2 (we upgraded from 3.x, where
> this used to work), although again I believe it was incorrect behavior
> and an `output_field` *should* be specified.

New description:

 Tested on Django 4.2.4.

 My model is defined like this:

 {{{
 class Choice(models.Model):
     choice_text = models.CharField(max_length=200)
     votes = models.IntegerField(default=0)
     arbitrary_num = models.PositiveIntegerField(default=1)
     arbitrary_num2 = models.PositiveIntegerField(default=2)
 }}}

 And this query is failing with Oracle (Tested on 19.3), but not Postgres:

 {{{
 s = Coalesce(F('arbitrary_num') + F('arbitrary_num2'),
                  Value(0, PositiveIntegerField()))
 case = Case(
         When(arbitrary_num=1, then=Value(1, PositiveIntegerField())),
         default=s,
         output_field=PositiveIntegerField()
 )

 qs = Choice.objects.annotate(ss=case)
 list(qs)

 #qs = Choice.objects.annotate(s=s)
 #list(qs)
 }}}

 The error (It's talking about the `Coalesce` function not having an
 `output_field`):

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

 I believe the error is correct, because I think adding two
 `PositiveIntegerField()` seem to result in an `IntegerField`, so we *need*
 to specify an `output_field` on `Coalesce` to tell Django the proper
 output type to use. My question is that this seem to only throw an error
 in Oracle, and *not* Postgres. So it seems like the behavior on Postgres
 is unexpected? Somehow, the `Case` statement swallows the exception in
 Postgres and generates correct result. This can be demonstrated by the
 queryset where the `Coalesce` function is annotated directly without a
 case statement -- in this case, both Postgres and Oracle fails with the
 above error.

 Note that this used to work prior to 4.2 (we upgraded from 3.x, where this
 used to work), although again I believe it was incorrect behavior and an
 `output_field` *should* be specified.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34858#comment:1>
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/0107018ab41b31a3-84e55db8-6362-45d7-9c13-f5d19dfa31b2-000000%40eu-central-1.amazonses.com.

Reply via email to