Skip to content

Commit 752762c

Browse files
authored
fix: sqlite decimal filter error with __gt (#2020)
1 parent fe41c34 commit 752762c

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Added
1616
^^^^^
1717
- Add `create()` method to reverse ForeignKey relations, enabling `parent.children.create()` syntax
1818

19+
Fixed
20+
^^^^^
21+
- Fix sqlite decimal filter error with `__gt` (#2020)
22+
1923
0.25
2024
====
2125

tests/fields/test_decimal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ async def test_filter(self):
6262
.first()
6363
)
6464
self.assertEqual(obj, obj0)
65+
objs = await testmodels.DecimalFields.filter(decimal_nodec__gt=2).all()
66+
self.assertIn(obj, objs)
67+
objs = await testmodels.DecimalFields.filter(decimal_nodec__lt=100).all()
68+
self.assertIn(obj, objs)
6569

6670
async def test_f_expression_update(self):
6771
obj0 = await testmodels.DecimalFields.create(decimal=Decimal("1.23456"), decimal_nodec=18.7)

tortoise/expressions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ def _process_filter_kwarg(
387387
else:
388388
filter_info = model._meta.get_filter(key)
389389

390+
field_object = None
390391
if "table" in filter_info:
391392
# join the table
392393
join = (
@@ -405,7 +406,14 @@ def _process_filter_kwarg(
405406
else field_object.to_db_value(value, model)
406407
)
407408
op = filter_info["operator"]
408-
criterion = op(table[filter_info.get("source_field", filter_info["field"])], value)
409+
term: Term = table[filter_info.get("source_field", filter_info["field"])]
410+
if field_object is not None:
411+
func = field_object.get_for_dialect(
412+
model._meta.db.capabilities.dialect, "function_cast"
413+
)
414+
if func is not None:
415+
term = func(field_object, term)
416+
criterion = op(term, value)
409417
return criterion, join
410418

411419
def _resolve_regular_kwarg(

0 commit comments

Comments
 (0)