Permalink
Browse files

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

Thanks Simon Charette for the fix.
  • Loading branch information...
1 parent 17ed0d3 commit f04495521ade8a2befc1aca70dd0a2c7aad4c987 @timgraham timgraham committed May 30, 2017
Showing with 17 additions and 2 deletions.
  1. +0 −1 django/db/models/expressions.py
  2. +3 −0 docs/releases/1.11.2.txt
  3. +6 −0 tests/expressions/models.py
  4. +8 −1 tests/expressions/tests.py
@@ -984,7 +984,6 @@ def as_sql(self, compiler, connection, template=None, **extra_context):
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
@@ -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`).
@@ -1,6 +1,7 @@
"""
Tests for F() query expression syntax.
"""
+import uuid
from django.db import models
@@ -79,8 +80,13 @@ def __str__(self):
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
@@ -24,7 +24,8 @@
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 @@ def test_in_subquery(self):
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))

0 comments on commit f044955

Please sign in to comment.