From 39b58ad95ade8109de0f0ae3930e333b7f689c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Modzelewski?= Date: Thu, 15 Jan 2015 22:16:25 +0100 Subject: [PATCH] Fixed #24148 -- Documented a bug with case expressions in SQLite < 3.7.0 --- docs/ref/databases.txt | 17 +++++++++++++++++ tests/expressions_case/tests.py | 8 +++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/ref/databases.txt b/docs/ref/databases.txt index c764095d9b..a3ad36f291 100644 --- a/docs/ref/databases.txt +++ b/docs/ref/databases.txt @@ -603,6 +603,23 @@ substring filtering. .. _documented at sqlite.org: http://www.sqlite.org/faq.html#q18 +Old SQLite and ``CASE`` expressions +----------------------------------- + +SQLite 3.6.23.1 and older contains a bug when `handling query parameters`_ in +a ``CASE`` expression that contains an ``ELSE`` and arithmetic. + +SQLite 3.6.23.1 was released in March 2010, and most current binary +distributions for different platforms include a newer version of SQLite, with +the notable exception of the Python 2.7 installers for Windows. + +As of this writing, the latest release for Windows - Python 2.7.9 - includes +SQLite 3.6.21. You can install ``pysqlite2`` or replace ``sqlite3.dll`` (by +default installed in ``C:\Python27\DLLs``) with a newer version from +http://www.sqlite.org/ to remedy this issue. + +.. _handling query parameters: https://code.djangoproject.com/ticket/24148 + .. _using-newer-versions-of-pysqlite: Using newer versions of the SQLite DB-API 2.0 driver diff --git a/tests/expressions_case/tests.py b/tests/expressions_case/tests.py index a344e79923..9636930e05 100644 --- a/tests/expressions_case/tests.py +++ b/tests/expressions_case/tests.py @@ -7,7 +7,7 @@ import unittest from uuid import UUID from django.core.exceptions import FieldError -from django.db import models +from django.db import connection, models from django.db.models import F, Q, Value, Min, Max from django.db.models.expressions import Case, When from django.test import TestCase @@ -254,6 +254,12 @@ class CaseExpressionTests(TestCase): transform=attrgetter('integer', 'test') ) + if connection.vendor == 'sqlite' and connection.Database.sqlite_version_info < (3, 7, 0): + # There is a bug in sqlite < 3.7.0, where placeholder order is lost. + # Thus, the above query returns + + # for each matching case instead of + 1 (#24148). + test_combined_expression = unittest.expectedFailure(test_combined_expression) + def test_in_subquery(self): self.assertQuerysetEqual( CaseTestModel.objects.filter(