编写一个将一些数据推送到 Postgres 数据库的 Node 应用程序。一列的数据类型是自定义复合类型的数组。
这是我要注入(inject)的内容:
'{(123, (12, "Some Description")),(null, (34, "Some Desc"))}'
复合类型有两个字段,第二个是另一个复合类型。我从 Postgres 返回的错误消息是:
malformed array literal: "{(123, (12, "Some Description")),(null, (34, "Some Desc"))}"
详细信息:
Unexpected array element
该列被声明为数据类型:
my_custom_data_type[]
无法弄清楚这个异常。
最佳答案
数组的字符串版本 ('{...}'
) 使用起来可能很麻烦,尤其是对于像您这样的更复杂的数组条目。我倾向于选择 array constructor syntax这样一切都是一个普通的旧值:
array[(123, (12, 'Some Description')), (null, (34, 'Some Desc'))]::my_custom_data_type[]
这种数组语法也倾向于更好地与 ORM 等一起工作。
::my_custom_data_type[]
类型转换有助于确保解析器知道您要说的内容,您应该能够只转换第一个条目:
array[(123, (12, 'Some Description'))::my_custom_data_type, (null, (34, 'Some Desc'))]
但转换整个数组可能更干净、更容易。
关于sql - PostgreSQL:意外的数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52087380/