--
Ticket URL: <https://code.djangoproject.com/ticket/18247>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: zbyte64 (added)
* needs_docs: => 0
* type: Uncategorized => Bug
* needs_tests: => 0
* needs_better_patch: => 0
Comment:
I believe I have experienced the same issue and it only seems to apply to
Django 1.4 with sqlite.
{{{#!python
from django.db import models
class Transaction(models.Model):
tag = models.CharField(max_length=5)
amount = models.DecimalField(max_digits=19, decimal_places=4)
def test_transaction():
Transaction.objects.all().delete()
Transaction.objects.create(amount=5, tag='a')
Transaction.objects.create(amount=4, tag='a')
Transaction.objects.create(amount=1, tag='b')
Transaction.objects.create(amount='1.5', tag='b')
Transaction.objects.create(amount=1, tag='c')
qs =
Transaction.objects.values('tag').annotate(sumamount=models.Sum('amount'))
print qs #displays 3 results
qs =
Transaction.objects.values('tag').annotate(sumamount=models.Sum('amount')).filter(sumamount__gt=3)
print qs #displays 0 results when using sqlite + Django 1.4, displays
1 result when using mysql or Django 1.3.1
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:1>
* status: new => closed
* resolution: => invalid
Comment:
I was able to replicate that on just sqlite3 (C API), so it is not a bug
with Django or driver.
Marking as invalid, as the bug is outside Django itself.
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:2>
* status: closed => reopened
* resolution: invalid =>
Comment:
Per http://article.gmane.org/gmane.comp.db.sqlite.general/75696 , SUM
values should not have affinity so typecasting Decimal into a string will
never return real values.
I think changing the typecast of Decimal into float is "good enough".
Internally, sqlite3 already represents these values as floats, and it
would fix this bug. Otherwise, we would need separate casting engines
depending on whether SUM is used.
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:3>
* stage: Unreviewed => Design decision needed
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:4>
* stage: Design decision needed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:6>
Comment (by zr):
#22117 was just closed as a duplicate of this ticket.
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:7>
* cc: dylan@… (added)
* version: 1.4 => master
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:8>
Comment (by dylan@…):
Replying to [comment:8 dylan@…]:
As noted in #22117 this is still an issue:
https://code.djangoproject.com/ticket/22117#comment:1
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:9>
Comment (by shai):
#22128 asked for the limitation to be documented; I closed it as a
duplicate of this. If this bug is wontfix'd, we should then add
appropriate warnings on the SQLite-specific notes.
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:10>
* cc: NEOatNHNG (added)
* keywords: decimal filter => decimal filter annotate having
* status: new => assigned
* owner: nobody => NEOatNHNG
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:11>
* has_patch: 0 => 1
Comment:
On sqlite the SUM() of a decimal column doesn't have a NUMERIC type so
when comparing it to a string literal (which a Decimal gets converted to
in django) it is not compared as NUMERIC as may be expected but as
something else (maybe string?).
I added a CAST to NUMERIC type when using a SQL function that is expected
to return a Decimal.
All tests pass under SQLite.
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:12>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:13>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"3bbaf84d6533fb61ac0038f2bbe52ee0d7b4fd10" 3bbaf84d]:
{{{
#!CommitTicketReference repository=""
revision="3bbaf84d6533fb61ac0038f2bbe52ee0d7b4fd10"
Fixed #18247 -- Added cast to NUMERIC for Decimals on sqlite
On sqlite the SUM() of a decimal column doesn't have a NUMERIC type so
when comparing it to a string literal (which a Decimal gets converted to
in Django) it is not compared as expected.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:14>
Comment (by suutari-ai):
Shouldn't this be backported to the current LTS release (1.8) too? The
commit seems to cherry-pick cleanly to stable/1.8.x branch.
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:15>
Comment (by Tim Graham <timograham@…>):
In [changeset:"068d75688f28f9a1530b771ae1d625f41e98cd82" 068d7568]:
{{{
#!CommitTicketReference repository=""
revision="068d75688f28f9a1530b771ae1d625f41e98cd82"
Refs #18247 -- Fixed SQLite QuerySet filtering on decimal result of Least
and Greatest.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:16>
Comment (by Tim Graham <timograham@…>):
In [changeset:"8484cf4cd0a6d3553f6fc1d62fa6dc04f04624f0" 8484cf4c]:
{{{
#!CommitTicketReference repository=""
revision="8484cf4cd0a6d3553f6fc1d62fa6dc04f04624f0"
[1.11.x] Refs #18247 -- Fixed SQLite QuerySet filtering on decimal result
of Least and Greatest.
Backport of 068d75688f28f9a1530b771ae1d625f41e98cd82 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:17>
Comment (by Tim Graham <timograham@…>):
In [changeset:"c3c6c92d769d44a98299c462c48a9599c0172e91" c3c6c92d]:
{{{
#!CommitTicketReference repository=""
revision="c3c6c92d769d44a98299c462c48a9599c0172e91"
Refs #18247 -- Fixed filtering on
CombinedExpression(output_field=DecimalField()) annotation on SQLite.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/18247#comment:18>