JsonField icontains filter generates invalid SQL

170 views
Skip to first unread message

cofiem

unread,
Apr 9, 2017, 9:48:57 PM4/9/17
to Django users
The issue I'm facing is similar to these tickets:
I have a JsonField in a model

data = JSONField(null=True, blank=True)

It contains this json:

{"name":"Hello"}

My filter is:

models.MyModel.objects.filter(data__name__icontains='el')

I expect the query to return the objects that have json where the "name" contains 'el' (case-insensitive).

Instead, I get this:

ProgrammingError at /url/
function upper(jsonb) does not exist LINE 1: ... UPPER("my_model"."data"... 
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

The SQL that causes the error is

 WHERE UPPER(("my_model"."data" -> 'name')::text) LIKE UPPER(%el%)

The working SQL is (note added quotes around LIKE text):

WHERE UPPER(("my_model"."data" -> 'name')::text) LIKE UPPER('%el%')

Is there a fix for this? Am I missing something?
Any help would be appreciated.

Simon Charette

unread,
Apr 10, 2017, 10:22:08 AM4/10/17
to Django users
Hello there,

I'm not sure where you determined the SQL that caused the error but I'm pretty
sure this was caused by "my_model"."data" -> 'name' not being wrapped in a
parenthesis before the ::text cast.

This has been fixed in Django 1.11[0].

Cheers,
Simon

Reply all
Reply to author
Forward
0 new messages