Refs #32096 -- Fixed ExpressionWrapper crash with JSONField key transforms.

Regression in 6789ded0a6.

Thanks Simon Charette and Igor Jerosimić for the report.
This commit is contained in:
Mariusz Felisiak 2020-10-14 11:09:49 +02:00
parent 7e1e198494
commit bbd55e5863
3 changed files with 20 additions and 2 deletions

View File

@ -920,7 +920,7 @@ class ExpressionWrapper(Expression):
return expression.get_group_by_cols(alias=alias) return expression.get_group_by_cols(alias=alias)
def as_sql(self, compiler, connection): def as_sql(self, compiler, connection):
return self.expression.as_sql(compiler, connection) return compiler.compile(self.expression)
def __repr__(self): def __repr__(self):
return "{}({})".format(self.__class__.__name__, self.expression) return "{}({})".format(self.__class__.__name__, self.expression)

View File

@ -30,3 +30,7 @@ Bugfixes
* Fixed a regression in Django 3.1 that caused a crash of ``__in`` lookup when * Fixed a regression in Django 3.1 that caused a crash of ``__in`` lookup when
using key transforms for :class:`~django.db.models.JSONField` in the lookup using key transforms for :class:`~django.db.models.JSONField` in the lookup
value (:ticket:`32096`). value (:ticket:`32096`).
* Fixed a regression in Django 3.1 that caused a crash of
:class:`~django.db.models.ExpressionWrapper` with key transforms for
:class:`~django.db.models.JSONField` (:ticket:`32096`).

View File

@ -10,7 +10,10 @@ from django.db import (
DataError, IntegrityError, NotSupportedError, OperationalError, connection, DataError, IntegrityError, NotSupportedError, OperationalError, connection,
models, models,
) )
from django.db.models import Count, F, OuterRef, Q, Subquery, Transform, Value from django.db.models import (
Count, ExpressionWrapper, F, IntegerField, OuterRef, Q, Subquery,
Transform, Value,
)
from django.db.models.expressions import RawSQL from django.db.models.expressions import RawSQL
from django.db.models.fields.json import ( from django.db.models.fields.json import (
KeyTextTransform, KeyTransform, KeyTransformFactory, KeyTextTransform, KeyTransform, KeyTransformFactory,
@ -405,6 +408,17 @@ class TestQuerying(TestCase):
[self.objs[4]], [self.objs[4]],
) )
def test_expression_wrapper_key_transform(self):
self.assertSequenceEqual(
NullableJSONModel.objects.annotate(
expr=ExpressionWrapper(
KeyTransform('c', 'value'),
output_field=IntegerField(),
),
).filter(expr__isnull=False),
self.objs[3:5],
)
def test_has_key(self): def test_has_key(self):
self.assertSequenceEqual( self.assertSequenceEqual(
NullableJSONModel.objects.filter(value__has_key='a'), NullableJSONModel.objects.filter(value__has_key='a'),