1
0
mirror of https://github.com/django/django.git synced 2025-10-27 15:46:10 +00:00

Fixed #36085 -- Added JSONField support for negative array indexing on SQLite.

This commit is contained in:
savanto
2025-05-13 13:42:58 +02:00
committed by Sarah Boyce
parent a8716f3c4c
commit 8620a3b0c7
8 changed files with 73 additions and 1 deletions

View File

@@ -347,6 +347,8 @@ class BaseDatabaseFeatures:
json_key_contains_list_matching_requires_list = False
# Does the backend support JSONObject() database function?
has_json_object_function = True
# Does the backend support negative JSON array indexing?
supports_json_negative_indexing = True
# Does the backend support column collations?
supports_collation_on_charfield = True

View File

@@ -793,6 +793,12 @@ class BaseDatabaseOperations:
# Hook for backends (e.g. NoSQL) to customize formatting.
return sqlparse.format(sql, reindent=True, keyword_case="upper")
def format_json_path_numeric_index(self, num):
"""
Hook for backends to customize array indexing in JSON paths.
"""
return "[%s]" % num
def compile_json_path(self, key_transforms, include_root=True):
"""
Hook for backends to customize all aspects of JSON path construction.
@@ -805,5 +811,13 @@ class BaseDatabaseOperations:
path.append(".")
path.append(json.dumps(key_transform))
else:
path.append("[%s]" % num)
if (
num < 0
and not self.connection.features.supports_json_negative_indexing
):
raise NotSupportedError(
"Using negative JSON array indices is not supported on this "
"database backend."
)
path.append(self.format_json_path_numeric_index(num))
return "".join(path)

View File

@@ -58,6 +58,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_stored_generated_columns = True
supports_virtual_generated_columns = True
supports_json_negative_indexing = False
@cached_property
def minimum_database_version(self):
if self.connection.mysql_is_mariadb:

View File

@@ -73,6 +73,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
requires_compound_order_by_subquery = True
allows_multiple_constraints_on_same_fields = False
supports_json_field_contains = False
supports_json_negative_indexing = False
supports_collation_on_textfield = False
test_now_utc_template = "CURRENT_TIMESTAMP AT TIME ZONE 'UTC'"
django_test_expected_failures = {

View File

@@ -441,3 +441,6 @@ class DatabaseOperations(BaseDatabaseOperations):
def force_group_by(self):
return ["GROUP BY TRUE"] if Database.sqlite_version_info < (3, 39) else []
def format_json_path_numeric_index(self, num):
return "[#%s]" % num if num < 0 else super().format_json_path_numeric_index(num)