,列 <columnName> 的类型为 jsonb 但表达式的类型为 text[]

,column <columnName> is of type jsonb but expression is of type text[]

有如下数组,需要保存在JSONB列:

[{"FoodType":"veg","pref":"High"}
,{"FoodType":"sea food","pref":"Medium"}
,{"FoodType":"Chicken","pref":"Low"}]

我只是传递 req.body 对象(来自 Express)以插入到数据库中。

db.one('insert into foodies(FoodieName, FoodPref,country,languagePref)' +
'values(${FoodieName}, $[FoodPref], ${country} ,${languagePref})  RETURNING FoodieId',req.body)

**通过 pg-promise 库的 PG DB 抛出错误:

{ [error: column "foodpref" is of type jsonb but expression is of type text[]]
name: 'error',
length: 196,
severity: 'ERROR',
code: '42804',
detail: undefined,
hint: 'You will need to rewrite or cast the expression.',
position: '123',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_target.c',
line: '529',
routine: 'transformAssignedExpr' }
POST /api/member 500 638.510 ms - 57

我认为它的驱动程序问题 cos array: function (arr) in formatting.js [in pg-promise lib] returns string 和 postgres 无法消化它。如果数组嵌套在任何对象中,那么它可以顺利运行,例如

"MyPref" : {Same Object array as above}

此处 MyPref 在 "FoodPref" 列中顺利通过。

If array is nested in any object then it works smoothly

这表明您没有正确传递格式化数据。您没有将 JSONB 的数据作为数组传递,而是传递了一个内部对象数组。

如果你把它作为一个对象传入属性,它就像你说的那样工作。要将其作为参数传递到数组中,您需要将其传递到 数组中 :

var data = [{"FoodType":"veg","pref":"High"}
,{"FoodType":"sea food","pref":"Medium"}
,{"FoodType":"Chicken","pref":"Low"}]

query('bla-bla ', [data])

即您的问题是您将其传递为:

query('bla-bla ', data)

将数组 - JSONB 数据误解为值数组 - 参数。

更新

.