我正在使用node-postgres进行一些更新,并且我希望返回未找到匹配项的任何值,因此我们知道什么不起作用,这就是我所拥有的:
let query = `
update ticket_custom_field t set
value = $1
where t.ticket_id = $2 AND t.custom_field_id = $3
returning $3 as "custom_field_id",$1 as "value";`;
results = await Promise.all(
custom_fields.map(cf => client.query(query,[cf.value,ticket_id,cf.custom_field_id]))
);
这在字段确实更新时有效:
"rows": [
{
"custom_field_id": 1,"value": "x1"
}
],"fields": [
{
"name": "custom_field_id","tableID": 0,"columnID": 0,"dataTypeID": 23,"dataTypeSize": 4,"dataTypeModifier": -1,"format": "text"
},{
"name": "value","dataTypeID": 25,"dataTypeSize": -1,"format": "text"
}
],
,但是当没有更新(例如,找不到字段ID)时,行数组为空。我有fields数组,但是并没有告诉我哪个更新失败,而无需手动分析结果并在之前/之后进行比较。