#32411: JSONField filter icontains is case sensitive in mysql
-------------------------------------+-------------------------------------
     Reporter:  elonzh               |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  3.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  JSONField            |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

 * cc: Mariusz Felisiak (added)
 * status:  new => closed
 * resolution:   => invalid


Comment:

 Hi. Thanks for the report.

 Your usage is wrong for what you're trying to do. See the
 [https://docs.djangoproject.com/en/3.2/topics/db/queries/#querying-
 jsonfield Querying JSONField topic docs].

 For JSONField `contains` takes key pairs, rather than a string:

 {{{
 >>> Dog.objects.filter(data__contains={'owner': 'Bob'})
 }}}

 `icontains` is not explicitly implemented on the field itself, so it's
 falling back to the base implementation (wrongly — the
 [https://database.guide/json_unquote-remove-quotes-from-a-json-document-
 in-mysql/ JSON_UNQUOTE] gives us a string but we didn't generate the
 `LOWER`) **but** it's not clear that makes much sense. (Mariusz: I'm
 closing this as invalid, but do we want to accept in order to raise an
 error here? 🤔)

 As you've discovered the correct usage is to query by field
 ([https://docs.djangoproject.com/en/3.1/topics/db/queries/#key-index-and-
 path-transforms as per here]):

 {{{
 >>> Dog.objects.filter(data__owner__name__icontains='bob')
 }}}

 For your example
 `Paper.objects.filter(authors__name__icontains="rehmann")` should give you
 what you want.
 I hope that helps.

 This seems related to the documentation issue #26511.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32411#comment:2>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.b9e24509c7bfb0dbffdffc3713c6a53d%40djangoproject.com.

Reply via email to