Opened 3 months ago
Last modified 12 days ago
#36085 assigned Bug
SQLite backend raises exception on negative array indices in JSONField
Description ¶
Positive jsonfield array index queries can be constructed the usual way with kwargs (ie. Model.objects.filter(jsonfield__2=1)
), while negative jsonfield array index query must be constructed using the splat operator on a dict: Model.objects.filter(**{"jsonfield__-2": 1})
). When querying jsonfields in this way on the SQLite backend, the following error arises:
django.db.utils.OperationalError: bad JSON path: '$[-2]'
When constructing the query, the JSON path constructor simply appends any numerical value to the path (https://github.com/django/django/blob/5.1.4/django/db/models/fields/json.py#L155). But the SQLite backend uses a special syntax for performing negative-indexing in jsonfields (https://sqlite.org/json1.html#path_arguments) that is different from other backends: negative indices must be prepended by a literal #
character.
According to the ticket's flags, the next step(s) to move this issue forward are:
- To improve the patch as described in the pull request review comments or on this ticket, then uncheck "Patch needs improvement".
If creating a new pull request, include a link to the pull request in the ticket comment when making that update. The usual format is:
[https://github.com/django/django/pull/#### PR]
.
Change History (8)
comment:1 by , 3 months ago
Has patch: | set |
---|
comment:2 by , 3 months ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 3 months ago
Cc: | added |
---|
comment:4 by , 6 weeks ago
Thanks savanto for your contributions!!
I hope you and tim graham and lay monage find my PR well done.
thanks again!!
comment:5 by , 6 weeks ago
Patch needs improvement: | unset |
---|
comment:6 by , 6 weeks ago
Patch needs improvement: | set |
---|
A PR was opened to start with a related refactor. It needs a little tweak.
comment:7 by , 2 weeks ago
Patch needs improvement: | unset |
---|
Since https://github.com/django/django/pull/19182 was closed without merge a few weeks ago, I incorporated the refactor work from it back into https://github.com/django/django/pull/19030 and made the suggested changes from discussions from both PRs there.
comment:8 by , 12 days ago
Patch needs improvement: | set |
---|
https://github.com/django/django/pull/19030