From ffac97bf63ea803a6196d2097b4186e0865d9091 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Wed, 17 Sep 2025 01:56:40 -0400 Subject: [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 af84cfba5970fda8306860b650937701c7c03c6f from main. --- django/db/models/fields/json.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'django') diff --git a/django/db/models/fields/json.py b/django/db/models/fields/json.py index d544caf3e5..e0aa5c622b 100644 --- a/django/db/models/fields/json.py +++ b/django/db/models/fields/json.py @@ -424,8 +424,12 @@ class KeyTextTransform(KeyTransform): output_field = TextField() def as_mysql(self, compiler, connection): - if connection.mysql_is_mariadb: - # MariaDB doesn't support -> and ->> operators (see MDEV-13594). + # The ->> operator is not supported on MariaDB (see MDEV-13594) and + # 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) return "JSON_UNQUOTE(%s)" % sql, params else: -- cgit v1.3