#29845: Casting FloatField to DecimalField causes MySQL/MariaDB SQL syntax error
-------------------------------------+-------------------------------------
     Reporter:  Ron                  |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.11
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

 * stage:  Unreviewed => Accepted


Old description:

> When I cast a FloatField to Decimal, I get an error from MySQL.
>
> Seen in django 1.11.16 on MariaDB.
>

> Model:
>

> {{{
> class MyModel(models.Model):
>  floatfield = models.FloatField('My FloatField')
>  decimalfield = models.DecimalField(_("My DecimalField"),
> decimal_places=2, max_digits=12,
> validators=[MinValueValidator(Decimal('0.01'))])
> }}}
>

> Query:
>

> {{{
> MyModel.objects.all().annotate(
>             floatfield_decimal=Cast('floatfield',
> DecimalField(max_digits=8, decimal_places=2))).aggregate(
>             revenue=Sum(F('floatfield_decimal') *
> F('decimalfield')))['revenue']
>
> }}}
>
> Then I see this error:
>

>
> {{{
> {ProgrammingError}(1064, "You have an error in your SQL syntax; check the
> manual that corresponds to your MariaDB server version for the right
> syntax to use near 'numeric(8, 2)) AS `floatfield_decimal`,
> `myclass`.`decimalfield' at line 1")
>
> }}}
>
> I guess the `numeric` should be `Decimal`... It's not possible to examite
> the query further from the debugger.

New description:

 When I cast a FloatField to Decimal, I get an error from MySQL.

 Seen in django 1.11.16 on MariaDB.

 Model:
 {{{
 class MyModel(models.Model):
     floatfield = models.FloatField()
     decimalfield = models.DecimalField(decimal_places=2, max_digits=12)
 }}}

 Query:
 {{{
 MyModel.objects.all().annotate(
     floatfield_decimal=Cast('floatfield', DecimalField(max_digits=8,
 decimal_places=2))).aggregate(
     revenue=Sum(F('floatfield_decimal') * F('decimalfield'))
 )['revenue']
 }}}

 Then I see this error:
 {{{
 {ProgrammingError}(1064, "You have an error in your SQL syntax; check the
 manual that corresponds to your MariaDB server version for the right
 syntax to use near 'numeric(8, 2)) AS `floatfield_decimal`,
 `myclass`.`decimalfield' at line 1")
 }}}

 I guess the `numeric` should be `Decimal`... It's not possible to examine
 the query further from the debugger.

--

Comment:

 Reproduced on master at 910548634a23f7a3346158e93de0ab308ae52c0c.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29845#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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.b230a70de6ab5cb5bfd4b5d505ee36b8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to