#27179: Bug: error when trying to filter using regex/iregex on a key in a
django.contrib.postgres.fields.JSONField
----------------------------------+------------------
     Reporter:  jrhouston         |      Owner:
         Type:  Bug               |     Status:  new
    Component:  contrib.postgres  |    Version:  1.10
     Severity:  Normal            |   Keywords:
 Triage Stage:  Unreviewed        |  Has patch:  0
Easy pickings:  1                 |      UI/UX:  0
----------------------------------+------------------
 When trying use regex to query on a key within a postgres JSONField.

 {{{
 TestModel.objects.filter(field__subkey__regex=r'test')
 }}}

 Generates SQL which produces the following error:

 {{{
 LINE 1: ...WHERE "test_testmodel"."field" -> 'subkey'::text ~ 'test'
 ^
 HINT:  No operator matches the given name and argument type(s). You might
 need to add explicit type casts.
 }}}

 This seems to be a precedence issue, and adding parenthesis before the
 extracted key is cast to `::text` allows the query to run successfully.
 i.e:

 {{{
 WHERE ("test_testmodel"."field" -> 'subkey')::text ~ 'test'
 }}}

 Adding the parenthesis here seems to fix the issue:
 
https://github.com/django/django/blob/master/django/contrib/postgres/fields/jsonb.py#L109

--
Ticket URL: <https://code.djangoproject.com/ticket/27179>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.34a0c35984871b4b9d0e4987d766091f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to