Skip to content

Implement json_contains filter for SQLite backend#2121

Open
Br1an67 wants to merge 2 commits intotortoise:developfrom
Br1an67:fix/json-contains-sqlite
Open

Implement json_contains filter for SQLite backend#2121
Br1an67 wants to merge 2 commits intotortoise:developfrom
Br1an67:fix/json-contains-sqlite

Conversation

@Br1an67
Copy link
Contributor

@Br1an67 Br1an67 commented Mar 1, 2026

Description

Implement the json_contains filter for the SQLite backend by registering a custom json_contains Python function on SQLite connections. The function implements JSON containment semantics matching PostgreSQL's @> operator:

  • Arrays: every element of the candidate appears in the target
  • Objects: every key-value pair of the candidate exists in the target
  • Scalars: equality check

Motivation and Context

Closes #1961.

Using data__contains on a JSONField with SQLite throws NotImplementedError: must be overridden in each executor. This makes it impossible to write database-agnostic code that filters JSON fields. PostgreSQL and MySQL both support this filter; this PR adds SQLite support.

How Has This Been Tested?

  • Updated test_list_contains in tests/fields/test_json.py to also run on SQLite (was previously restricted to MySQL and PostgreSQL)
  • All 15 JSON field tests pass (5 skipped are for contained_by/filter which remain unimplemented for SQLite)
  • Ran ruff lint and format checks — all pass

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

abondar and others added 2 commits February 9, 2026 00:22
Register a custom json_contains function on SQLite connections that
implements JSON containment semantics matching PostgreSQL's @> operator.
Arrays check element-wise inclusion, objects check key-value subset,
and scalars check equality.

Closes tortoise#1961
def _json_contains_impl(target_str: str | None, candidate_str: str | None) -> bool:
"""Check if target JSON value contains the candidate JSON value.

Semantics match PostgreSQL's @> operator:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be referencing postgresql implementation in sqlite contrib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

json_contains NotImplementedError with SQLite - only works with PostgreSQL

2 participants