[Django] #17854: DecimalField problem

56 views
Skip to first unread message

Django

unread,
Mar 8, 2012, 6:20:27 AM3/8/12
to django-...@googlegroups.com
#17854: DecimalField problem
---------------------------------+------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.3
Severity: Release blocker | Keywords: DecimalField bug
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------
For a model field created as :: models.DecimalField(max_digits = 200,
decimal_places = 100, blank = False, null = False)
While using admin interface to insert a record involving such a
DecimalField, the format changes (loss of precision and it uses scientific
notation (even in the database)) (Please note - it works properly for low
precision values (example - .987654321001234) - but for larger precision
values (probably 15 decimal_places or more) it results in loss of
precision)

django version 1.3.1 and 1.4c1 (don't know about older versions);
python 2.6.6;
linux;

--
Ticket URL: <https://code.djangoproject.com/ticket/17854>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 8, 2012, 7:10:52 AM3/8/12
to django-...@googlegroups.com
#17854: DecimalField problem
----------------------------------+--------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.3
Severity: Release blocker | Resolution:
Keywords: DecimalField bug | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by anonymous):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

The database is sqlite3

The database field (for sqlite3) is 'decimal' (when the field type in the
database is changed to say varchar(n) where n > (values to be stored (in
this case probably 202)) - everything works perfectly.

The problem seems to be that django emits the column type as decimal for
sqlite3 (for other databases this problem is untested) instead of
varchar(max_length + probably 2)

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:1>

Django

unread,
Mar 8, 2012, 7:13:16 AM3/8/12
to django-...@googlegroups.com
#17854: DecimalField problem
----------------------------------+--------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.3
Severity: Release blocker | Resolution:
Keywords: DecimalField bug | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by anonymous):

Replying to [comment:1 anonymous]:

> The database is sqlite3
>
> The database field (for sqlite3) is 'decimal' (when the field type in
the database is changed to say varchar(n) where n > (values to be stored
(in this case probably 202)) - everything works perfectly.
>
> The problem seems to be that django emits the column type as decimal for
sqlite3 (for other databases this problem is untested) instead of
varchar(max_length + probably 2)

typo resolution for the above line == instead of varchar(max_digits +
probably 2)

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:2>

Django

unread,
Mar 8, 2012, 7:55:42 AM3/8/12
to django-...@googlegroups.com
#17854: DecimalField problem
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Release blocker | Triage Stage:
Keywords: DecimalField bug | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by anonymous):

* component: Uncategorized => Database layer (models, ORM)


Comment:

Replying to [comment:2 anonymous]:

> Replying to [comment:1 anonymous]:
> > The database is sqlite3
> >
> > The database field (for sqlite3) is 'decimal' (when the field type in
the database is changed to say varchar(n) where n > (values to be stored
(in this case probably 202)) - everything works perfectly.
> >
> > The problem seems to be that django emits the column type as decimal
for sqlite3 (for other databases this problem is untested) instead of
varchar(max_length + probably 2)
>
> typo resolution for the above line == instead of varchar(max_digits +
probably 2)

if does not create any side effect(s) a change to this might be the
solution === django/db/backends/sqlite3/creation.py

also similar change to respective backends might be the solution (in case
a database has limits (unlike python Decimals) on the scale, precision for
its corresponding column type)

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:3>

Django

unread,
Mar 8, 2012, 7:56:31 AM3/8/12
to django-...@googlegroups.com
#17854: DecimalField problem (please see a possible solution in comments)
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Release blocker | Triage Stage:
Keywords: DecimalField bug | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:4>

Django

unread,
Mar 8, 2012, 8:34:18 AM3/8/12
to django-...@googlegroups.com
#17854: Problem with DecimalField and big vlues of max_digits, decimal_places,
sqlite3 backend
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Release blocker | Triage Stage:
Keywords: DecimalField bug | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:5>

Django

unread,
Mar 8, 2012, 9:40:58 AM3/8/12
to django-...@googlegroups.com
#17854: Problem with DecimalField and big vlues of max_digits, decimal_places,
sqlite3 backend
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: DecimalField bug | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ramiro):

* severity: Release blocker => Normal


--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:6>

Django

unread,
Mar 12, 2012, 5:46:03 PM3/12/12
to django-...@googlegroups.com
#17854: Problem with DecimalField and big vlues of max_digits, decimal_places,
sqlite3 backend
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: DecimalField bug | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by tmitchell):

* stage: Unreviewed => Accepted


Comment:

This is not specific to the admin, as the attached patch shows. This
patch works under postgres but fails under sqlite3.

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:7>

Django

unread,
Mar 13, 2012, 4:31:31 AM3/13/12
to django-...@googlegroups.com
#17854: Problem with DecimalField and big vlues of max_digits, decimal_places,
sqlite3 backend
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: DecimalField bug | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by anonymous):

This problem exists for postgres and mysql too (along with sqlite3).

for postgres - limit is up to 131072 digits before the decimal point; up
to 16383 digits after the decimal point (from postgres website -
http://www.postgresql.org/docs/9.1/static/datatype-numeric.html#DATATYPE-
NUMERIC-TABLE)

similarly docs on the mysql site mention that a decimal field has a limit
of 65 decimal places (but I do not know anything else as I haven't used
mysql)

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:8>

Django

unread,
Mar 13, 2012, 7:37:34 AM3/13/12
to django-...@googlegroups.com
#17854: Problem with DecimalField and big vlues of max_digits, decimal_places,
sqlite3 backend
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: DecimalField bug | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Old description:

> For a model field created as :: models.DecimalField(max_digits = 200,
> decimal_places = 100, blank = False, null = False)
> While using admin interface to insert a record involving such a
> DecimalField, the format changes (loss of precision and it uses
> scientific notation (even in the database)) (Please note - it works
> properly for low precision values (example - .987654321001234) - but for
> larger precision values (probably 15 decimal_places or more) it results
> in loss of precision)
>
> django version 1.3.1 and 1.4c1 (don't know about older versions);
> python 2.6.6;
> linux;

New description:

For a model field created as
{{{
models.DecimalField(max_digits = 200, decimal_places = 100, blank = False,
null = False)
}}}

While using admin interface to insert a record involving such a
!DecimalField, the format changes (loss of precision and it uses
scientific notation (even in the database)) (Please note - it works
properly for low precision values (example - .987654321001234) - but for
larger precision values (probably 15 decimal_places or more) it results in
loss of precision)

* django version 1.3.1 and 1.4c1 (don't know about older versions);
* python 2.6.6;
* linux;

--

Comment (by ramiro):

Would be acceptable to "fix" this by documenting the limitations of the
three database engine/adaptors in the DB notes document?

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:9>

Django

unread,
Jan 16, 2013, 10:49:13 AM1/16/13
to django-...@googlegroups.com
#17854: Problem with DecimalField and big vlues of max_digits, decimal_places,
sqlite3 backend
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: DecimalField bug | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by wdoekes):

* cc: wdoekes (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:10>

Django

unread,
Jan 16, 2013, 11:02:28 AM1/16/13
to django-...@googlegroups.com
#17854: Problem with DecimalField and big vlues of max_digits, decimal_places,
sqlite3 backend
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: DecimalField bug | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by wdoekes):

I was assuming the problem is the same as the one I encountered. But
perhaps this belongs in a different ticket.

The decimal type is cast as string when constructing a query. But for
larger values, MySQL doesn't cast the string to decimal properly. Instead
it becomes a lossy float.

That means you get this:
{{{
mysql> select 12345678901234567 as value, convert('12345678901234567' +
0.0, decimal(31,0)) as truncated_value;
+-------------------+-------------------+
| value | truncated_value |
+-------------------+-------------------+
| 12345678901234567 | 12345678901234570 |
+-------------------+-------------------+
1 row in set (0.01 sec)
}}}

When looking up a value, then comparison fails.

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:11>

Django

unread,
Jan 17, 2013, 6:16:15 AM1/17/13
to django-...@googlegroups.com
#17854: Problem with DecimalField and big vlues of max_digits, decimal_places,
sqlite3 backend
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: DecimalField bug | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by wdoekes):

Never mind my comments. They belong in a different ticket: #19625

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:12>

Django

unread,
Aug 4, 2015, 11:02:16 AM8/4/15
to django-...@googlegroups.com
#17854: Problem with DecimalField and big vlues of max_digits, decimal_places,
sqlite3 backend
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) |
Severity: Normal | Resolution:
Keywords: DecimalField bug | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by wdoekes):

* Attachment "django-1.1.x-bug17854-decimalfield-casts.patch​" removed.

REMOVED, IRRELEVANT

Django

unread,
Aug 4, 2015, 11:03:50 AM8/4/15
to django-...@googlegroups.com
#17854: Problem with DecimalField and big vlues of max_digits, decimal_places,
sqlite3 backend
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) |
Severity: Normal | Resolution:
Keywords: DecimalField bug | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by wdoekes):

* Attachment "django-1.1.x-bug17854-decimalfield-casts.patch" removed.

Possible fix against very old django.

Django

unread,
Aug 4, 2015, 11:04:43 AM8/4/15
to django-...@googlegroups.com
#17854: Problem with DecimalField and big values of max_digits, decimal_places
-------------------------------------+-------------------------------------

Reporter: anonymous | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.3
(models, ORM) |
Severity: Normal | Resolution:
Keywords: DecimalField bug | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:11>

Django

unread,
Oct 1, 2015, 1:26:52 PM10/1/15
to django-...@googlegroups.com
#17854: Add database-specific checks for the maximum supported values of
DecimalField max_digits, decimal_places
--------------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: Core (System checks) | Version: master

Severity: Normal | Resolution:
Keywords: DecimalField bug | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* type: Bug => New feature
* version: 1.3 => master
* component: Database layer (models, ORM) => Core (System checks)


Comment:

I think we could add database-specific checks (e.g.
`django/db/backends/mysql/validation.py`) for the maximum values of
`max_digits`, `decimal_places` supported by each database.

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:12>

Django

unread,
Jul 19, 2025, 9:01:29 AMJul 19
to django-...@googlegroups.com
#17854: Add database-specific checks for the maximum supported values of
DecimalField max_digits, decimal_places
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Yu Chen
| Lee
Type: New feature | Status: assigned
Component: Core (System | Version: dev
checks) |
Severity: Normal | Resolution:
Keywords: DecimalField bug | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Yu Chen Lee):

* has_patch: 0 => 1
* owner: nobody => Yu Chen Lee
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:13>

Django

unread,
Jul 31, 2025, 10:14:00 AMJul 31
to django-...@googlegroups.com
#17854: Add database-specific checks for the maximum supported values of
DecimalField max_digits, decimal_places
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: Yu Chen
| Lee
Type: New feature | Status: assigned
Component: Core (System | Version: dev
checks) |
Severity: Normal | Resolution:
Keywords: DecimalField bug | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/17854#comment:14>
Reply all
Reply to author
Forward
0 new messages