First differing element 1:
(2, 3)
(2, 2)
- [(1, 3), (2, 3), (3, 4), (2, 3), (3, 4), (3, 4), (4, 4)]
? ^ ^
+ [(1, 3), (2, 2), (3, 4), (2, 2), (3, 4), (3, 4), (4, 4)]
? ^ ^
}}}
No problem on Python 3.4.
--
Ticket URL: <https://code.djangoproject.com/ticket/24148>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: michalmo (added)
Comment:
Michał, any chance you have access to Windows and can reproduce this? If
not, I can give you access to a system if you're willing to help debug
this.
--
Ticket URL: <https://code.djangoproject.com/ticket/24148#comment:1>
* owner: nobody => michalmo
* status: new => assigned
Comment:
Reproduced. I'll look into it.
--
Ticket URL: <https://code.djangoproject.com/ticket/24148#comment:2>
Comment (by michalmo):
I've had a look and it's not pretty. The SQL generated in pythons 2.7,
3.2, 3.3 and 3.4 on windows is exactly the same.
sqlite3.version is 2.6.0 on all of them.
What differs is sqlite3.sqlite_version:
py2.7: 3.6.21
py3.2: 3.7.4
py3.3: 3.7.12
py3.4: 3.8.3.1
I installed pysqlite from http://www.lfd.uci.edu/~gohlke/pythonlibs/
(2.6.3 / 3.8.5) and the test passes.
What is happening? SQLite is adding the value in the lookup to the value
in the then.
You can see this better with a different example (with the same test data
as the testcase):
{{{
>>> qs = CaseTestModel.objects.annotate(
... test=Case(
... When(integer=1, then=Value(1)),
... When(integer=2, then=Value(1)),
... When(integer=3, then=Value(1)),
... When(integer=4, then=Value(10)),
... default=Value(1),
... output_field=models.IntegerField(),
... ) + 100,
... ).order_by('pk')
>>> # expected: [(1, 101), (2, 101), (3, 101), (2, 101), (3, 101), (3,
101), (4, 110)]
>>> [(o.integer, o.test) for o in qs]
[(1, 2), (2, 3), (3, 4), (2, 3), (3, 4), (3, 4), (4, 14)]
}}}
Removing the default fixes it (since all the cases are taken care of)
{{{
>>> qs = CaseTestModel.objects.annotate(
... test=Case(
... When(integer=1, then=Value(1)),
... When(integer=2, then=Value(1)),
... When(integer=3, then=Value(1)),
... When(integer=4, then=Value(10)),
... # default=Value(1),
... output_field=models.IntegerField(),
... ) + 100,
... ).order_by('pk')
>>> # expected: [(1, 101), (2, 101), (3, 101), (2, 101), (3, 101), (3,
101), (4, 110)]
>>> [(o.integer, o.test) for o in qs]
[(1, 101), (2, 101), (3, 101), (2, 101), (3, 101), (3, 101), (4, 110)]
}}}
Also, if I send the query as a string without the placeholders it works.
So I assume that SQLite 3.6.21 is mixing up the query parameters. Not sure
if the error is specific to this version, but it seems there was a query
related bug fix in 3.6.22
(http://www.sqlite.org/changes.html#version_3_6_22 ).
Unfortunately, I checked all the windows python versions from 2.7a1 to
2.7.9 and they all have the same sqlite3.sqlite_version, so there's
nothing we can do here other than mark the test as an expected failure and
recommend pysqlite on windows.
--
Ticket URL: <https://code.djangoproject.com/ticket/24148#comment:3>
Comment (by timgraham):
That resolution is fine with me.
--
Ticket URL: <https://code.djangoproject.com/ticket/24148#comment:4>
* has_patch: 0 => 1
Comment:
I've done some testing, and SQLite 3.7.0 is the first version that passes
this test case.
PR is ready.
--
Ticket URL: <https://code.djangoproject.com/ticket/24148#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"39b58ad95ade8109de0f0ae3930e333b7f689c0f"]:
{{{
#!CommitTicketReference repository=""
revision="39b58ad95ade8109de0f0ae3930e333b7f689c0f"
Fixed #24148 -- Documented a bug with case expressions in SQLite < 3.7.0
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24148#comment:6>