Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#29881 closed Bug (duplicate)

Invalid SQL on MySQL with Cast to DecimalField

Reported by: Par Andersson Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Normal Keywords: mysql, decimal, numeric
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Par Andersson)

Casting to DecimalField triggers a bug in MySQL due to DecimalField being mapped to numeric.

Example:

Model.objects.annotate(bug=Cast('somefield', DecimalField(10,2)))

Will generate an error from MySQL, the issue is that MySQL has an issue where it's not possible to cast to numeric as reported here:
https://bugs.mysql.com/bug.php?id=84558

With Django this happens due to the data type mapping defined in data_types (db/backends/mysql/base.py:111 to be exact):

'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',

I propose that this line is changed to:

'DecimalField': 'decimal(%(max_digits)s, %(decimal_places)s)',

as this seems to be a simple work around for the underlying issue.

Change History (3)

comment:1 by Par Andersson, 6 years ago

Description: modified (diff)

comment:2 by Tim Graham, 6 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #29845.

comment:3 by Tim Graham, 6 years ago

Summary: Decimal data type mapping triggers bug in MySQLInvalid SQL on MySQL with Cast to DecimalField
Note: See TracTickets for help on using tickets.
Back to Top