#27693: Case insensitive lookups on JSONField nested values
--------------------------------------------+------------------------
               Reporter:  Harris Lapiroff   |          Owner:  (none)
                   Type:  New feature       |         Status:  new
              Component:  contrib.postgres  |        Version:  1.10
               Severity:  Normal            |       Keywords:
           Triage Stage:  Unreviewed        |      Has patch:  0
    Needs documentation:  0                 |    Needs tests:  0
Patch needs improvement:  0                 |  Easy pickings:  0
                  UI/UX:  0                 |
--------------------------------------------+------------------------
 Currently `JSONField` doesn't support case-insensitive lookups such as
 `iexact`. This is not documented as working, so I'm marking this as a new
 feature, but it's something I think Django ''can'' and should support.

 Attempting to use an `iexact` lookup (e.g.,
 `OrganizationPerson.objects.filter(data__field2__iexact='Ethelfort')`)
 results in a SQL error:

 {{{
 django.db.utils.ProgrammingError: function upper(jsonb) does not exist
 LINE 1: ...st" FROM "organizations_organizationperson" WHERE UPPER("org...
                                                              ^
 HINT:  No function matches the given name and argument types. You might
 need to add explicit type casts.
 }}}

 I believe this is just a case of misplaced parens and quotes. Similar
 functionality (though still not documented) ''does'' work on
 `HStoreField`s. Here's an example of the successful SQL Django generates
 for an `iexact` lookup on an `HStoreField`.

 {{{
 SELECT "organizations_organizationperson"."id",
 "organizations_organizationperson"."organization_id",
 "organizations_organizationperson"."person_id",
 "organizations_organizationperson"."data",
 "organizations_organizationperson"."test" FROM
 "organizations_organizationperson" WHERE
 UPPER(("organizations_organizationperson"."test" -> 'foo')::text) =
 UPPER('Bar') LIMIT 21
 }}}

 And here's the ''unsuccessful'' SQL Django generates for an `iexact`
 lookup on a `JSONField`, which generates the error above:

 {{{
 SELECT "organizations_organizationperson"."id",
 "organizations_organizationperson"."organization_id",
 "organizations_organizationperson"."person_id",
 "organizations_organizationperson"."data",
 "organizations_organizationperson"."test" FROM
 "organizations_organizationperson" WHERE
 UPPER("organizations_organizationperson"."data" -> 'field2'::text) =
 UPPER('Ethelfort') LIMIT 21
 }}}

 I don't have an ORM-level fix for correcting the query, but a proper query
 that works as expected just includes a couple more parens and quotes in
 the `WHERE` clause:

 {{{
 SELECT "organizations_organizationperson"."id",
 "organizations_organizationperson"."organization_id",
 "organizations_organizationperson"."person_id",
 "organizations_organizationperson"."data",
 "organizations_organizationperson"."test" FROM
 "organizations_organizationperson" WHERE
 UPPER(("organizations_organizationperson"."data" -> 'field2')::text) =
 UPPER('"Ethelfort"') LIMIT 21
 }}}

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/057.3f00c04d55481ed8043c67452ec03994%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to