Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28915 closed Bug (fixed)

DecimalField truncates trailing zeros in the fractional part on SQLite

Reported by: Raphael Michel Owned by: Sergey Fedoseev
Component: Database layer (models, ORM) Version: 2.0
Severity: Release blocker Keywords:
Cc: Sergey Fedoseev Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With Django 1.11, when reading the value of a DecimalField from the database, the Decimal is always returned with the precision of the DecimalField, i.e. if the field has 2 decimal places, I always get a Decimal with two decimal places as well.

With Django 2.0, I get a rounded decimal with no more decimal places.

Sample:

obj = Foo.objects.create(a="bar", d=Decimal('8.320'))
obj.refresh_from_db()

print(repr(obj.d)) 

This will output Decimal('8.320') on Django 1.11 but Decimal('8.32') on Django 2.0.

For me (and likely many others) this is quite critical: For example, if you use DecimalFields to store amounts of money in a currency that always has two places, you can just pass the databases to localize() and get a user-friendly representation. This is no longer possible, as you would first need to call quantize in every single place which would be quite an effort to do in a large codebasis.

Since there is no mention of this in the release notes, I believe this is an unwanted regression and hope it can be fixed in 2.0.2 or the like.

I wrote a regression test for the problem and ran git bisect to identify that the problem was introduced in commit a146b6562.

I did not find a way to keep the performance gain of this commit without introducing this regression, so I strongly ask to revert that commit altogether for now (and add my regression test, if wanted) until someone has a better idea how to optimize this.

Change History (8)

comment:1 by Raphael Michel, 7 years ago

Summary: Regression in value of DecimalFields on SQLite in Django 2.0Regression in handling of DecimalField values on SQLite in Django 2.0

comment:2 by Simon Charette, 7 years ago

Cc: Sergey Fedoseev added
Triage Stage: UnreviewedAccepted

Thank you for your detailed report Raphael.

Could you confirm the issue is only present on SQLite? How do other database backend behave in this regard?

comment:3 by Sergey Fedoseev, 7 years ago

Owner: changed from nobody to Sergey Fedoseev
Status: newassigned

comment:4 by Sergey Fedoseev, 7 years ago

Has patch: set

comment:5 by Tim Graham, 7 years ago

Summary: Regression in handling of DecimalField values on SQLite in Django 2.0DecimalField truncates trailing zeros in the fractional part on SQLite
Triage Stage: AcceptedReady for checkin

comment:6 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 6fd6d838:

Fixed #28915 -- Prevented SQLite from truncating trailing zeros in the fractional part of DecimalField.

This reverts commit a146b65628e702a9a3ed5be21542ca45366fbb29 and adds
a test for the regression.

comment:7 by Tim Graham <timograham@…>, 7 years ago

In 0f7ca1e8:

[2.0.x] Fixed #28915 -- Prevented SQLite from truncating trailing zeros in the fractional part of DecimalField.

This reverts commit a146b65628e702a9a3ed5be21542ca45366fbb29 and adds
a test for the regression.

Backport of 6fd6d8383f48ea2fe4e058725fa30529a083e9a5 from master

comment:8 by Tim Graham <timograham@…>, 7 years ago

In 7c939ae:

[2.0.x] Refs #28932 -- Skipped the failing test for refs #28915 on Oracle.

Note: See TracTickets for help on using tickets.
Back to Top