Description: I have DecimalField with `max_digits=4`. Previously, querying
for something larger (e.g. 12345) would execute the sql and return
ObjectNotFound. Now, in 4.2, it throws a decimal.InvalidOperation error,
as it tries to quantize the value to have 4 digits.
I understand that it doesn't make sense to query for a larger number, but
the error that occurs was pretty confusing to me. Also, it is not as easy
to check in my application, because I don't have easy access to the
`max_digits` parameter of the field.
In my opinion, the backend should either accept larger values and always
return "not found", or the error should be more descriptive, so that it
can be caught specifically.
Testcase: placed in `tests/backends` folder and used for git bisect
{{{
import decimal
from django.db import models
from django.test import TestCase
class DecimalModel(models.Model):
dec_field = models.DecimalField(decimal_places=0, max_digits=4)
class InvalidDecimalQuery(TestCase):
def test_invalid_decimal_query(self):
try:
DecimalModel.objects.get(dec_field='12345')
except decimal.InvalidOperation:
self.fail("Too large decimal query caused exception.")
except DecimalModel.DoesNotExist:
pass
}}}
Stacktrace:
{{{
Traceback (most recent call last):
File "lib/python3.10/site-packages/django/db/models/manager.py", line
87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "lib/python3.10/site-packages/django/db/models/query.py", line 633,
in get
num = len(clone)
File "lib/python3.10/site-packages/django/db/models/query.py", line 380,
in __len__
self._fetch_all()
File "lib/python3.10/site-packages/django/db/models/query.py", line
1881, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "lib/python3.10/site-packages/django/db/models/query.py", line 91,
in __iter__
results = compiler.execute_sql(
File "lib/python3.10/site-packages/django/db/models/sql/compiler.py",
line 1547, in execute_sql
sql, params = self.as_sql()
File "lib/python3.10/site-packages/django/db/models/sql/compiler.py",
line 762, in as_sql
self.compile(self.where) if self.where is not None else ("", [])
File "lib/python3.10/site-packages/django/db/models/sql/compiler.py",
line 544, in compile
sql, params = node.as_sql(self, self.connection)
File "lib/python3.10/site-packages/django/db/models/sql/where.py", line
145, in as_sql
sql, params = compiler.compile(child)
File "lib/python3.10/site-packages/django/db/models/sql/compiler.py",
line 544, in compile
sql, params = node.as_sql(self, self.connection)
File "lib/python3.10/site-packages/django/db/models/lookups.py", line
357, in as_sql
return super().as_sql(compiler, connection)
File "lib/python3.10/site-packages/django/db/models/lookups.py", line
225, in as_sql
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
File "lib/python3.10/site-packages/django/db/models/lookups.py", line
126, in process_rhs
return self.get_db_prep_lookup(value, connection)
File "lib/python3.10/site-packages/django/db/models/lookups.py", line
254, in get_db_prep_lookup
else [get_db_prep_value(value, connection, prepared=True)],
File "lib/python3.10/site-packages/django/db/models/fields/__init__.py",
line 1761, in get_db_prep_value
return connection.ops.adapt_decimalfield_value(
File "lib/python3.10/site-
packages/django/db/backends/base/operations.py", line 574, in
adapt_decimalfield_value
return utils.format_number(value, max_digits, decimal_places)
File "lib/python3.10/site-packages/django/db/backends/utils.py", line
304, in format_number
value = value.quantize(
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34590>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: Florian Apolloner (added)
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
Comment:
Confirmed regression with 7990d254b0af158baf827fafbd90fe8e890f23bd
Crashes with sqlite, postgres ok
--
Ticket URL: <https://code.djangoproject.com/ticket/34590#comment:1>