有如下数组,需要保存在 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 数组:formatting.js [in pg-promise lib] 中的函数(arr)返回字符串并且 postgres 无法消化它。如果数组嵌套在任何对象中,那么它可以顺利工作,例如
"MyPref" : {Same Object array as above}
这里 MyPref 在“FoodPref”列中通过,没有任何问题。