After upgrading from version 3.1 to version 3.2, I found precision problem in using decimal field. Following example is a small code which could regenerate problem.
from django.db import models
class Foo(models.Model):
amount = models.DecimalField(
null=False,
decimal_places=18,
max_digits=44)
# A simple insertion example
from api.models import Foo
from django.db.models import F
from decimal import Decimal
f = Foo.objects.create(amount=Decimal('0.5'))
f.amount = F('amount') - Decimal('0.4')
f.save(update_fields=['amount', ])
f.refresh_from_db()
print(f.amount)
This code generates 0.099999999999999980
but same code in previous version generate expected result 0.01
. By checking mysql query generated for update query, I found that in new version number has sent using a quotation but in previous one there were no quotation. i.e in new version we have something like this UPDATE `api_foo` SET `amount` = (`api_foo`.`amount` - '0.4') WHERE `api_foo`.`id` = 1
but in previous one we had UPDATE `api_foo` SET `amount` = (`api_foo`.`amount` - 0.4) WHERE `api_foo`.`id` = 1
. Also I must mention that this happens just in mysql driver not in sqllite.
I know this seems like a bug, but I guessed that I am missing something trivial.
I am using mysql-8.0.19 and I got the same value in versions 3.2 and 3.2.3 of django.