Closed lanzz closed 9 years ago
Apparently other lookup types are also subtly broken:
Model.objects.filter(jsonfield__at_key__in=['foo', 'bar'])
generates this SQL:
SELECT ... FROM "app_model" WHERE "app_model"."jsonfield"->>'key' IN ('"foo"', '"bar"')
As you can see, the values being matched are JSON-encoded (there are extra quotes added within the strings), so that condition will match if jsonfield->>'key'
is "foo"
(a string of five characters, including the quotes), but will not match if it is just foo
(a string of three characters).
Two pull requests for both issues described here: https://github.com/djangonauts/django-pgjson/pull/13 https://github.com/djangonauts/django-pgjson/pull/14
Merged and released 0.2.3 ;) thanks!
I'm expecting this filter to return records that have a non-null value in the
jsonfield->>'key'
field. The generated SQL is however wrong:According to the PostgreSQL operator precedence table, the
->>
operator has a lower precedence than theIS
operator, so the above condition is actually parsed as"jsonfield"->>('key' IS NOT NULL)
, while it should be("jsonfield"->>'key') IS NOT NULL
.