0

Need to check if jsonb column contains and not contains value. Jsonb field is a simple array:

ALTER TABLE task ADD COLUMN worker_ids jsonb;

UPDATE task SET worker_ids = '["1", "2"]' WHERE ...

Then trying to run queries to check either it contains or not contains concrete value:

Query which checks if jsonb contains value - is working fine and returns correct set of rows

 SELECT * FROM task WHERE worker_ids ? '1'

However when I add NOT to the where condition query returns simply nothing:

 SELECT * FROM task WHERE NOT worker_ids ? '1'

Am I getting something wrong ?

4
  • worker_ids array contains 1, so why are you expecting a result? Commented Apr 20, 2018 at 11:23
  • Are the other worker_ids fields NULL? Commented Apr 20, 2018 at 11:34
  • comparison against null gives null, not true or false, so not null ? value is not false and thus not returned Commented Apr 20, 2018 at 11:38
  • @eurotrash true :) I thought it is obvious because asking why no rows returned for false would be strange when you know they all are true. but yes, you are right - it's logical, not obvious Commented Apr 20, 2018 at 11:40

1 Answer 1

3

https://www.postgresql.org/docs/current/static/functions-comparison.html

Ordinary comparison operators yield null (signifying “unknown”), not true or false, when either input is null. For example, 7 = NULL yields null, as does 7 <> NULL. When this behavior is not suitable, use the IS [ NOT ] DISTINCT FROM predicates:

something like:

SELECT * FROM task WHERE (worker_ids ? '1') is distinct from true

should work

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.