Skip to content

Commit

Permalink
[3.2.x] Fixed #32793 -- Fixed loss of precision for temporal operatio…
Browse files Browse the repository at this point in the history
…ns with DecimalFields on MySQL.

Regression in 1e38f11.

Thanks Mohsen Tamiz for the report.
Backport of e703b15 from main
  • Loading branch information
felixxm committed Jun 1, 2021
1 parent b2ff165 commit 94675a7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions django/db/backends/mysql/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ def force_no_ordering(self):
"""
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.
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/3.2.4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
3 changes: 2 additions & 1 deletion tests/expressions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ def __str__(self):
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):
Expand Down
7 changes: 7 additions & 0 deletions tests/expressions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,13 @@ def test_complex_expressions(self):
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
Expand Down

0 comments on commit 94675a7

Please sign in to comment.