mirror of
https://github.com/django/django.git
synced 2025-10-26 15:16:09 +00:00
[6.0.x] Fixed #36612 -- Fixed a KeyTextTransform crash on MySQL against annotations.
MySQL only supports the ->> when used directly against columns, this can be
inferred by the presence of lhs.output_field.model as model bounds fields are
directly tied to columns.
Purposely don't systematically switch to using JSON_QUOTE(JSON_EXTRACT(...))
as there might be functional indices out there that rely on the SQL remaining
stable between versions.
Thanks Jacob Tavener for the report.
Backport of af84cfba59 from main.
This commit is contained in:
committed by
Sarah Boyce
parent
359c1c6ff9
commit
ffac97bf63
@@ -424,8 +424,12 @@ class KeyTextTransform(KeyTransform):
|
|||||||
output_field = TextField()
|
output_field = TextField()
|
||||||
|
|
||||||
def as_mysql(self, compiler, connection):
|
def as_mysql(self, compiler, connection):
|
||||||
if connection.mysql_is_mariadb:
|
# The ->> operator is not supported on MariaDB (see MDEV-13594) and
|
||||||
# MariaDB doesn't support -> and ->> operators (see MDEV-13594).
|
# only supported against columns on MySQL.
|
||||||
|
if (
|
||||||
|
connection.mysql_is_mariadb
|
||||||
|
or getattr(self.lhs.output_field, "model", None) is None
|
||||||
|
):
|
||||||
sql, params = super().as_mysql(compiler, connection)
|
sql, params = super().as_mysql(compiler, connection)
|
||||||
return "JSON_UNQUOTE(%s)" % sql, params
|
return "JSON_UNQUOTE(%s)" % sql, params
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -1160,6 +1160,12 @@ class TestQuerying(TestCase):
|
|||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_cast_with_key_text_transform(self):
|
||||||
|
obj = NullableJSONModel.objects.annotate(
|
||||||
|
json_data=Cast(Value({"foo": "bar"}, JSONField()), JSONField())
|
||||||
|
).get(pk=self.objs[0].pk, json_data__foo__icontains="bar")
|
||||||
|
self.assertEqual(obj, self.objs[0])
|
||||||
|
|
||||||
@skipUnlessDBFeature("supports_json_field_contains")
|
@skipUnlessDBFeature("supports_json_field_contains")
|
||||||
def test_contains_contained_by_with_key_transform(self):
|
def test_contains_contained_by_with_key_transform(self):
|
||||||
tests = [
|
tests = [
|
||||||
|
|||||||
Reference in New Issue
Block a user