Opened 6 years ago

Closed 6 years ago

#29845 closed Bug (fixed)

Casting to DecimalField causes MySQL/MariaDB SQL syntax error

Reported by: Ronny Vedrilla Owned by: Mariusz Felisiak
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 (last modified by Tim Graham)

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.

Change History (7)

comment:1 by Tim Graham, 6 years ago

Description: modified (diff)
Summary: django orm cast float field to decimal causes MySQL errorCasting FloatField to DecimalField causes MySQL/MariaDB SQL syntax error
Triage Stage: UnreviewedAccepted

Reproduced on master at 910548634a23f7a3346158e93de0ab308ae52c0c.

comment:2 by Mariusz Felisiak, 6 years ago

Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:3 by Mariusz Felisiak, 6 years ago

Summary: Casting FloatField to DecimalField causes MySQL/MariaDB SQL syntax errorCasting to DecimalField causes MySQL/MariaDB SQL syntax error

comment:4 by Mariusz Felisiak, 6 years ago

Has patch: set

comment:5 by Ronny Vedrilla, 6 years ago

Thanks!

comment:6 by Tim Graham, 6 years ago

Triage Stage: AcceptedReady for checkin

comment:7 by GitHub <noreply@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In f2e2a1b:

Fixed #29845 -- Fixed Cast crash on MySQL when casting to DecimalField.

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