Fixed #29155 -- Fixed crash when database functions are used with pattern lookups.

Thanks Tim Graham and Sergey Fedoseev for reviews.
This commit is contained in:
Mariusz Felisiak 2018-03-14 10:00:07 +01:00 committed by GitHub
parent 8f75984c26
commit feb683c4c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

View File

@ -397,7 +397,7 @@ class PatternLookup(BuiltinLookup):
def process_rhs(self, qn, connection):
rhs, params = super().process_rhs(qn, connection)
if params and not self.bilateral_transforms:
if self.rhs_is_direct_value() and params and not self.bilateral_transforms:
params[0] = self.param_pattern % connection.ops.prep_for_like_query(params[0])
return rhs, params

View File

@ -17,6 +17,7 @@ class Alarm(models.Model):
class Author(models.Model):
name = models.CharField(max_length=100)
alias = models.CharField(max_length=50, null=True, blank=True)
class Meta:
ordering = ('name',)

View File

@ -5,6 +5,7 @@ from operator import attrgetter
from django.core.exceptions import FieldError
from django.db import connection
from django.db.models.functions import Substr
from django.test import TestCase, skipUnlessDBFeature
from .models import Article, Author, Game, Player, Season, Tag
@ -878,3 +879,19 @@ class LookupTests(TestCase):
season = Season.objects.create(year=2012, nulled_text_field=None)
self.assertTrue(Season.objects.filter(pk=season.pk, nulled_text_field__isnull=True))
self.assertTrue(Season.objects.filter(pk=season.pk, nulled_text_field=''))
def test_pattern_lookups_with_substr(self):
a = Author.objects.create(name='John Smith', alias='Johx')
b = Author.objects.create(name='Rhonda Simpson', alias='sonx')
tests = (
('startswith', [a]),
('istartswith', [a]),
('contains', [a, b]),
('icontains', [a, b]),
('endswith', [b]),
('iendswith', [b]),
)
for lookup, result in tests:
with self.subTest(lookup=lookup):
authors = Author.objects.filter(**{'name__%s' % lookup: Substr('alias', 1, 3)})
self.assertCountEqual(authors, result)