mirror of https://github.com/django/django.git
Fixed #28967 -- Prevented Cast to FloatField from rounding to integer on MySQL.
This commit is contained in:
parent
f1aa58479c
commit
44908d4d93
|
@ -20,7 +20,6 @@ class DatabaseOperations(BaseDatabaseOperations):
|
||||||
'IntegerField': 'signed integer',
|
'IntegerField': 'signed integer',
|
||||||
'BigIntegerField': 'signed integer',
|
'BigIntegerField': 'signed integer',
|
||||||
'SmallIntegerField': 'signed integer',
|
'SmallIntegerField': 'signed integer',
|
||||||
'FloatField': 'signed',
|
|
||||||
'PositiveIntegerField': 'unsigned integer',
|
'PositiveIntegerField': 'unsigned integer',
|
||||||
'PositiveSmallIntegerField': 'unsigned integer',
|
'PositiveSmallIntegerField': 'unsigned integer',
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,11 @@ class Cast(Func):
|
||||||
extra_context['db_type'] = self.output_field.cast_db_type(connection)
|
extra_context['db_type'] = self.output_field.cast_db_type(connection)
|
||||||
return super().as_sql(compiler, connection, **extra_context)
|
return super().as_sql(compiler, connection, **extra_context)
|
||||||
|
|
||||||
|
def as_mysql(self, compiler, connection):
|
||||||
|
# MySQL doesn't support explicit cast to float.
|
||||||
|
template = '(%(expressions)s + 0.0)' if self.output_field.get_internal_type() == 'FloatField' else None
|
||||||
|
return self.as_sql(compiler, connection, template=template)
|
||||||
|
|
||||||
def as_postgresql(self, compiler, connection):
|
def as_postgresql(self, compiler, connection):
|
||||||
# CAST would be valid too, but the :: shortcut syntax is more readable.
|
# CAST would be valid too, but the :: shortcut syntax is more readable.
|
||||||
return self.as_sql(compiler, connection, template='%(expressions)s::%(db_type)s')
|
return self.as_sql(compiler, connection, template='%(expressions)s::%(db_type)s')
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
import decimal
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.expressions import Value
|
from django.db.models.expressions import Value
|
||||||
|
@ -55,5 +56,7 @@ class CastTests(TestCase):
|
||||||
self.assertEqual(dates.get().cast_datetime, now)
|
self.assertEqual(dates.get().cast_datetime, now)
|
||||||
|
|
||||||
def test_cast_from_python(self):
|
def test_cast_from_python(self):
|
||||||
numbers = Author.objects.annotate(cast_float=Cast(0, models.FloatField()))
|
numbers = Author.objects.annotate(cast_float=Cast(decimal.Decimal(0.125), models.FloatField()))
|
||||||
self.assertEqual(numbers.get().cast_float, 0.0)
|
cast_float = numbers.get().cast_float
|
||||||
|
self.assertIsInstance(cast_float, float)
|
||||||
|
self.assertEqual(cast_float, 0.125)
|
||||||
|
|
Loading…
Reference in New Issue