I have a table which has a jsonB column named as emailAddress
. An example row in that column is like this:
{
"[email protected]": {
"tags": {
"work": true
}
},
"[email protected]": {
"tags": {
"work": true
}
}
}
I want to create a new column named lookup
which is generated based on the keys of the emailAddress
column. So the value of lookup
column of the above example should be something like:
[email protected] [email protected]
This will obviously update if more keys are added to the emailAddress
column.
I started with this query:
ALTER TABLE "table_example"
ADD COLUMN lookup TEXT GENERATED ALWAYS AS (jsonb_object_keys("emailAddresses")) STORED;
But the error I get is:
ERROR: set-returning functions are not allowed in column generation expressions
I understand that this is not the right way to do it because no where I m specifying the separator
I want to use. I tried using multiple functions like array()
or array_agg()
to get this to a form accepted by generated column, but doesn’t seem to work.
Any ideas?
2
Answers
This will work fine.
Better build a view instead. Use lateral join
or a scalar subquery