postgresql - how to query using postgres json field where a key is not set? -
i can select records match json value where properties->>'foo' = 'bar'
, if key 'foo' has not yet been set? i've tried where properties->>'foo' null
error
no operator matches given name , argument type(s). might need add together explicit type casts. : select "merchants".* "merchants" (properties->>'foo' null)
it's operator precedence issue. is null
binds more tightly ->>
, code beingness read properties ->> ('foo' null)
. add together parentheses - (properties ->> 'foo') null
.
regress=> select '{"a":1}' ->> 'a'; ?column? ---------- 1 (1 row) regress=> select '{"a":1}' ->> 'b'; ?column? ---------- (1 row) regress=> select '{"a":1}' ->> 'b' null; error: operator not exist: unknown ->> boolean line 1: select '{"a":1}' ->> 'b' null; ^ hint: no operator matches given name , argument type(s). might need add together explicit type casts. regress=> select ('{"a":1}' ->> 'b') null; ?column? ---------- t (1 row)
json postgresql
No comments:
Post a Comment