Opened 43 minutes ago

Last modified 35 minutes ago

#37275 new Bug

DecimalField with no precision doesn't work on SQLite

Reported by: Matt Cooper Owned by:
Component: Database layer (models, ORM) Version: 6.1
Severity: Normal Keywords: decimal
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Matt Cooper)

Django 6.1 introduced DecimalFields without precision specifiers. SQLite is called out as a platform where this mode is supported, but objects stored with precision-less DecimalFields can't be retrieved from the database.

# models.py
from django.db import models

class DecimalWithPrecision(models.Model):
    value = models.DecimalField(max_digits=4, decimal_places=2)

class DecimalWithoutPrecision(models.Model):
    value = models.DecimalField()
# pyproject.toml
[project]
name = "repro-noprecision-decimal-failure"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.14"
dependencies = [
    "django==6.1.0",
]
# repro in REPL; linebreaks added for clarity and stack trace lightly anonymized

>>> [DecimalWithPrecision(value=3.14).save(), DecimalWithoutPrecision(value=3.14).save()]
[None, None]

>>> DecimalWithPrecision.objects.first()
<DecimalWithPrecision: DecimalWithPrecision object (1)>

>>> DecimalWithoutPrecision.objects.first()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/repro-noprecision-decimal-failure/.venv/lib/python3.14/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/repro-noprecision-decimal-failure/.venv/lib/python3.14/site-packages/django/db/models/query.py", line 1189, in first
    for obj in queryset[:1]:
               ~~~~~~~~^^^^
  File "/repro-noprecision-decimal-failure/.venv/lib/python3.14/site-packages/django/db/models/query.py", line 434, in __iter__
    self._fetch_all()
    ~~~~~~~~~~~~~~~^^
  File "/repro-noprecision-decimal-failure/.venv/lib/python3.14/site-packages/django/db/models/query.py", line 2239, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/repro-noprecision-decimal-failure/.venv/lib/python3.14/site-packages/django/db/models/query.py", line 130, in __iter__
    for row in compiler.results_iter(results):
               ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/repro-noprecision-decimal-failure/.venv/lib/python3.14/site-packages/django/db/models/sql/compiler.py", line 1577, in results_iter
    converters = self.get_converters(fields)
  File "/repro-noprecision-decimal-failure/.venv/lib/python3.14/site-packages/django/db/models/sql/compiler.py", line 1525, in get_converters
    backend_converters = self.connection.ops.get_db_converters(expression)
  File "/repro-noprecision-decimal-failure/.venv/lib/python3.14/site-packages/django/db/backends/sqlite3/operations.py", line 282, in get_db_converters
    converters.append(self.get_decimalfield_converter(expression))
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/repro-noprecision-decimal-failure/.venv/lib/python3.14/site-packages/django/db/backends/sqlite3/operations.py", line 320, in get_decimalfield_converter
    -expression.output_field.decimal_places
TypeError: bad operand type for unary -: 'NoneType'

Change History (1)

comment:1 by Matt Cooper, 35 minutes ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top