diff --git a/django/db/models/fields/json.py b/django/db/models/fields/json.py index 1b219e620c..8d9b20659d 100644 --- a/django/db/models/fields/json.py +++ b/django/db/models/fields/json.py @@ -385,10 +385,16 @@ class KeyTransform(Transform): datatype_values = ",".join( [repr(datatype) for datatype in connection.ops.jsonfield_datatype_values] ) - return ( - "(CASE WHEN JSON_TYPE(%s, %%s) IN (%s) " - "THEN JSON_TYPE(%s, %%s) ELSE JSON_EXTRACT(%s, %%s) END)" - ) % (lhs, datatype_values, lhs, lhs), (tuple(params) + (json_path,)) * 3 + if connection.get_database_version() >= (3, 38): + return ( + "(CASE WHEN JSON_TYPE(%s, %%s) IN (%s) " + "THEN JSON_TYPE(%s, %%s) ELSE %s ->> %%s END)" + ) % (lhs, datatype_values, lhs, lhs), (tuple(params) + (json_path,)) * 3 + else: + return ( + "(CASE WHEN JSON_TYPE(%s, %%s) IN (%s) " + "THEN JSON_TYPE(%s, %%s) ELSE JSON_EXTRACT(%s, %%s) END)" + ) % (lhs, datatype_values, lhs, lhs), (tuple(params) + (json_path,)) * 3 class KeyTextTransform(KeyTransform):