Opened 6 years ago

Last modified 6 years ago

#29845 closed Bug

django orm cast float field to decimal causes MySQL error — at Initial Version

Reported by: Ronny Vedrilla Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top