Fixed #32793 -- Fixed loss of precision for temporal operations with DecimalFields on MySQL.

Regression in 1e38f1191d.

Thanks Mohsen Tamiz for the report.
This commit is contained in:
Mariusz Felisiak 2021-06-01 15:11:42 +02:00 committed by GitHub
parent a0410ffe8f
commit e703b152c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 1 deletions

View File

@ -160,6 +160,9 @@ class DatabaseOperations(BaseDatabaseOperations):
"""
return [(None, ("NULL", [], False))]
def adapt_decimalfield_value(self, value, max_digits=None, decimal_places=None):
return value
def last_executed_query(self, cursor, sql, params):
# With MySQLdb, cursor objects have an (undocumented) "_executed"
# attribute where the exact query sent to the database is saved.

View File

@ -28,3 +28,6 @@ Bugfixes
* Fixed a regression in Django 3.2 that caused a crash of auto-reloader with
``AttributeError``, e.g. inside a ``Conda`` environment (:ticket:`32783`).
* Fixed a regression in Django 3.2 that caused a loss of precision for
operations with ``DecimalField`` on MySQL (:ticket:`32793`).

View File

@ -48,9 +48,10 @@ class Company(models.Model):
class Number(models.Model):
integer = models.BigIntegerField(db_column='the_integer')
float = models.FloatField(null=True, db_column='the_float')
decimal_value = models.DecimalField(max_digits=20, decimal_places=17, null=True)
def __str__(self):
return '%i, %.3f' % (self.integer, self.float)
return '%i, %.3f, %.17f' % (self.integer, self.float, self.decimal_value)
class Experiment(models.Model):

View File

@ -1194,6 +1194,13 @@ class ExpressionsNumericTests(TestCase):
self.assertEqual(Number.objects.get(pk=n.pk).integer, 10)
self.assertEqual(Number.objects.get(pk=n.pk).float, Approximate(256.900, places=3))
def test_decimal_expression(self):
n = Number.objects.create(integer=1, decimal_value=Decimal('0.5'))
n.decimal_value = F('decimal_value') - Decimal('0.4')
n.save()
n.refresh_from_db()
self.assertEqual(n.decimal_value, Decimal('0.1'))
class ExpressionOperatorTests(TestCase):
@classmethod