从 hstore 中删除密钥时出现意外的字符串结尾

Unexpected end of string when removing key from hstore

当我从 HSTORE 中删除密钥时,出现错误 'Unexpected end of string':

DB=# UPDATE mytable SET properties = properties - 'key'  where label = '9912345678';
ERROR:  Unexpected end of string
LINE 1: UPDATE mytable SET properties = properties - 'key'  where ...

当我显式转换该字符串时,它确实有效:

DB=# UPDATE mytable SET properties = properties - 'key'::text  where label = '9912345678';
UPDATE 1

为什么会出现这个错误信息? 'key' 不是 TEXT 列吗?或者至少是一个带有预期结尾的字符串?

运算符已重载,因此应显式转换右侧操作数。请注意 the documentation:

texttext[]hstore 的示例用法
'a=>1, b=>2, c=>3'::hstore - 'b'::text
'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b']
'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore

在声明中

UPDATE mytable SET properties = properties - 'key' where label = '9912345678';

字符串 'key' 可能被解析为 texthstore。 hstore算法的首选是hstore,所以语句解析为

UPDATE mytable SET properties = properties - 'key'::hstore where label = '9912345678';

并引发错误

ERROR:  Unexpected end of string

尽管错误消息可能会提供更多信息,例如 Unexpected end of string while parsing hstore constant.