Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The posgres filter "not contains" doesn't work as I expect

I am doing the following query:

SELECT *
FROM "Organization"
WHERE "allowedDomains" @> '{"gmail.com"}'
AND NOT "blacklist" @> '{"example@gmail.com"}';

I expect this query to list all organizations that have gmail.com in the allowedDomains (string[]) and NOT to include the organization if that organization has in blacklist (string[]) example@gmail.com.

When I do:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT *
FROM "Organization"
WHERE "allowedDomains" @> '{"gmail.com"}'

I get

enter image description here

And then I add the AND NOT "blacklist" @> '{"example@gmail.com"}';

And get 0 results.

(Just to clarify, I am just using example@gmail.com to redact the email, the query email I’m trying to filter is correctly typed)

>Solution :

If any of two arrays is null the result of the operator @> is also null. Use coalesce().

SELECT *
FROM "Organization"
WHERE "allowedDomains" @> '{"gmail.com"}'
AND NOT coalesce("blacklist", '{}') @> '{"example@gmail.com"}';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading