Changes between Initial Version and Version 1 of Ticket #34858


Ignore:
Timestamp:
Sep 20, 2023, 2:39:55 PM (12 months ago)
Author:
Toan Vuong
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #34858 – Description

    initial v1  
    33My model is defined like this:
    44
    5 ```
     5{{{
    66class Choice(models.Model):
    77    choice_text = models.CharField(max_length=200)
     
    99    arbitrary_num = models.PositiveIntegerField(default=1)
    1010    arbitrary_num2 = models.PositiveIntegerField(default=2)
    11 ```
     11}}}
    1212
    1313And this query is failing with Oracle (Tested on 19.3), but not Postgres:
    1414
    15 ```
     15{{{
    1616s = Coalesce(F('arbitrary_num') + F('arbitrary_num2'),
    1717                 Value(0, PositiveIntegerField()))
     
    2727#qs = Choice.objects.annotate(s=s)
    2828#list(qs)
    29 
    30 ```
     29}}}
    3130
    3231The error (It's talking about the `Coalesce` function not having an `output_field`):
    3332
    34 ```
     33{{{
    3534django.core.exceptions.FieldError: Expression contains mixed types: IntegerField, PositiveIntegerField. You must set output_field.
    36 ```
     35}}}
    3736
    3837I 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.
Back to Top