[Django] #32921: Incorrect SQL generation for JSON has_key__in

16 views
Skip to first unread message

Django

unread,
Jul 13, 2021, 8:39:50 AM7/13/21
to django-...@googlegroups.com
#32921: Incorrect SQL generation for JSON has_key__in
-------------------------------------+-------------------------------------
Reporter: Shaheed | Owner: nobody
Haque |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have a Django 3.2.4 model with a JSONField (hosted on Postgres 12)
called "snapshot" which contains employees like this:
{{{
{
"employee": {
"6": {...},
"99": {...}
}
}
}}}
I would like to perform a .filter() on the presence of certain employee
keys, so I tried this:
{{{
qs = company.payrun_set.filter(snapshot__employee__has_key__in=['6',
'8'])
}}}
but this produces incorrect results. When I examine qs.query (which I know
does not always produce an exact rendition), I see the following
incorrect SQL:
{{{
...("paiyroll_payrun"."snapshot" #> ['employee', 'has_key']) IN ("6",
"8")
}}}
Notice how **has_key** is treated as a literal key value. I believe the
correct query can be formed like this:
{{{
qs = company.payrun_set.extra(where=[''' "paiyroll_payrun"."snapshot"
#> '{employee}' ?| array['6', '8'] '''])
}}}
and the corresponding SQL fragment should then look like this (again from
examining qs.query):
{{{
..."paiyroll_payrun"."snapshot"#>'{employee}' ?| array['6', '8']
}}}
Basically, **has_key** followed by **in** should be converted into the "?|
array[]".

I'm not an expert, so would love to be corrected if I have misunderstood.

Thanks, Shaheed

--
Ticket URL: <https://code.djangoproject.com/ticket/32921>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 13, 2021, 9:56:07 AM7/13/21
to django-...@googlegroups.com
#32921: Incorrect SQL generation for JSON has_key__in
-------------------------------------+-------------------------------------
Reporter: Shaheed Haque | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Shaheed Haque):

Also, it turns out that an empty "array[]" gives rise to an error:
{{{
django.db.utils.ProgrammingError: cannot determine type of empty array
LINE 1: ...021-06-12'::date AND (snapshot #> '{employee}' ?| array[])) ...
^
HINT: Explicitly cast to the desired type, for example
ARRAY[]::integer[].
}}}
which I fixed by casting to an array of strings using **::varchar[]**
(since JSON keys are always strings) like this:


{{{
qs = company.payrun_set.extra(where=[''' "paiyroll_payrun"."snapshot" #>

'{employee}' ?| array[...is empty...]::varchar[] '''])
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32921#comment:1>

Django

unread,
Jul 13, 2021, 11:27:18 AM7/13/21
to django-...@googlegroups.com
#32921: Incorrect SQL generation for JSON has_key__in
-------------------------------------+-------------------------------------
Reporter: Shaheed Haque | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

`has_key` cannot be chained with other lookups, if you do this it will be
interpreted as a key lookup for `JSONField` as you observed. You should
use [https://docs.djangoproject.com/en/3.2/topics/db/queries/#has-any-keys
has_key_any]. If you're having trouble understanding how Django works, see
TicketClosingReasons/UseSupportChannels for ways to get help.

--
Ticket URL: <https://code.djangoproject.com/ticket/32921#comment:2>

Reply all
Reply to author
Forward
0 new messages