[英]postgres GENERATED ALWAYS jsonb with refers to other columns inside a string
Trying to generate a jsonb column that will look like this: ["c:cluster-x", "ns:cluster-x/namespace-1"]
尝试生成如下所示的 jsonb 列: ["c:cluster-x", "ns:cluster-x/namespace-1"]
The cluster and namespace will be taken from other fields.集群和命名空间将取自其他字段。 I'm struggling with finding a way to concat it successfully我正在努力寻找一种成功连接它的方法
Also an array literal will be fine here, like this '{c:cluster-x, ns:cluster-x/namespace-1}'
这里也可以使用数组文字,例如'{c:cluster-x, ns:cluster-x/namespace-1}'
I tried something like this:我试过这样的事情:
ALTER TABLE my_table ADD COLUMN resources jsonb GENERATED ALWAYS AS ('["c:"' || my_table."clusterName" || ']'::jsonb) STORED;
but getting: Detail: Expected JSON value, but found "]".
但得到: Detail: Expected JSON value, but found "]".
postgres version 13.4 postgres 版本 13.4
You are casting a single character ]
as jsonb.您将单个字符]
转换为 jsonb。 That's why you're getting this error.这就是您收到此错误的原因。 Also, the :
is missing (misplaced?) between key and value.此外,键和值之间缺少:
(错位?)。
This should work:这应该有效:
ALTER TABLE my_table ADD COLUMN resources jsonb
GENERATED ALWAYS AS (('[{"c":"' || "clusterName" || '"}]')::jsonb) STORED;
Note : This solution will return NULL if the column clusterName
is NULL.注意:如果列clusterName
为 NULL,此解决方案将返回 NULL。
Demo: db<>fiddle
演示: db<>fiddle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.