Skip to content

Commit

Permalink
[3.1.x] Refs #31829 -- Added DatabaseFeatures.json_key_contains_list_…
Browse files Browse the repository at this point in the history
…matching_requires_list.

CockroachDB's behavior matches PostgreSQL.
Backport of 184a6ee from master
  • Loading branch information
timgraham authored and felixxm committed Jul 30, 2020
1 parent df8696c commit 32cb1fe
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions django/db/backends/base/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ class BaseDatabaseFeatures:
# Does the backend support __contains and __contained_by lookups for
# a JSONField?
supports_json_field_contains = True
# Does value__d__contains={'f': 'g'} (without a list around the dict) match
# {'d': [{'f': 'g'}]}?
json_key_contains_list_matching_requires_list = False

def __init__(self, connection):
self.connection = connection
Expand Down
1 change: 1 addition & 0 deletions django/db/backends/postgresql/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
validates_explain_options = False # A query will error on invalid options.
supports_deferrable_unique_constraints = True
has_json_operators = True
json_key_contains_list_matching_requires_list = True

@cached_property
def is_postgresql_9_6(self):
Expand Down
5 changes: 3 additions & 2 deletions tests/model_fields/test_jsonfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,9 @@ def test_contains_contained_by_with_key_transform(self):
)),
),
]
# PostgreSQL requires a layer of nesting.
if connection.vendor != 'postgresql':
# For databases where {'f': 'g'} (without surrounding []) matches
# [{'f': 'g'}].
if not connection.features.json_key_contains_list_matching_requires_list:
tests.append(('value__d__contains', {'f': 'g'}))
for lookup, value in tests:
with self.subTest(lookup=lookup, value=value):
Expand Down

0 comments on commit 32cb1fe

Please sign in to comment.