I have occasion and relation tables. relation.occasions is a json type field, which contains an array of occasion keys. I want to get occasion records by relation, where occasion.key in relation.occasions. Assume, that this is my DB data:
occasion
key | name |
-------------------------------------
BIRTHDAY | Birthday |
ANNIVERSARY | Anniversary |
relation
key | occasions |
------------------------------------------------------
FATHER | [ "BIRTHDAY", "ANNIVERSARY" ] |
FRIEND | [ "BIRTHDAY" ] |
Here is the query I'm trying to use:
SELECT * FROM occasion o WHERE o.key IN
(SELECT json_array_elements(r.occasions)::text from relation r WHERE r.key = 'FATHER')
The result is 0 rows instead of expected 2. Can somebody give me a hint what am I doing wrong? How else can I achieve desired result?