Fixed #28199 -- Fixed Subquery generating unnecessary/invalid CAST.

Thanks Simon Charette for the fix.
This commit is contained in:
Tim Graham 2017-05-30 06:40:41 -04:00 committed by GitHub
parent 17ed0d36bc
commit f04495521a
4 changed files with 17 additions and 2 deletions

View File

@ -984,7 +984,6 @@ class Subquery(Expression):
template = template or template_params.get('template', self.template)
sql = template % template_params
sql = connection.ops.unification_cast_sql(self.output_field) % sql
return sql, sql_params
def _prepare(self, output_field):

View File

@ -39,3 +39,6 @@ Bugfixes
* Fixed ``MultipleObjectMixin.paginate_queryset()`` crash on Python 2 if the
``InvalidPage`` message contains non-ASCII (:ticket:`28204`).
* Prevented ``Subquery`` from adding an unnecessary ``CAST`` which resulted in
invalid SQL (:ticket:`28199`).

View File

@ -1,6 +1,7 @@
"""
Tests for F() query expression syntax.
"""
import uuid
from django.db import models
@ -79,8 +80,13 @@ class SimulationRun(models.Model):
return "%s (%s to %s)" % (self.midpoint, self.start, self.end)
class UUIDPK(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
class UUID(models.Model):
uuid = models.UUIDField(null=True)
uuid_fk = models.ForeignKey(UUIDPK, models.CASCADE, null=True)
def __str__(self):
return "%s" % self.uuid

View File

@ -24,7 +24,8 @@ from django.test import (
from django.test.utils import Approximate
from .models import (
UUID, Company, Employee, Experiment, Number, Result, SimulationRun, Time,
UUID, UUIDPK, Company, Employee, Experiment, Number, Result, SimulationRun,
Time,
)
@ -479,6 +480,12 @@ class BasicExpressionsTests(TestCase):
subquery_test2 = Company.objects.filter(pk=Subquery(small_companies.filter(num_employees=3)))
self.assertCountEqual(subquery_test2, [self.foobar_ltd])
def test_uuid_pk_subquery(self):
u = UUIDPK.objects.create()
UUID.objects.create(uuid_fk=u)
qs = UUIDPK.objects.filter(id__in=Subquery(UUID.objects.values('uuid_fk__id')))
self.assertCountEqual(qs, [u])
def test_nested_subquery(self):
inner = Company.objects.filter(point_of_contact=OuterRef('pk'))
outer = Employee.objects.annotate(is_point_of_contact=Exists(inner))