PG-Promise - "Property doesn't exist" 跳过函数出错
PG-Promise - "Property doesn't exist" error with skip function
我无法弄清楚如何利用 skip
跳过 undefined/null 值。我不断收到 Error: Property 'vehicle_id' doesn't exist
。列集中的 skip
和 upsertReplaceQuery()
的 skip
是否相互冲突?我怎样才能让它工作?
const vehicleColumnSet = new pgp.helpers.ColumnSet(
[
{ name: 'user_id' },
{
name: 'vehicle_id',
skip: (c) => !c.exists,
},
{ name: 'model_id', def: null },
],
{ table: 'vehicle' }
);
const upsertReplaceQuery = (data, columnSet, conflictField) => {
return `${pgp.helpers.insert(
data,
columnSet
)} ON CONFLICT(${conflictField}) DO UPDATE SET ${columnSet.assignColumns({
from: 'EXCLUDED',
skip: conflictField,
})}`;
};
const vehicleUpsertQuery = upsertReplaceQuery(
{
user_id,
model_id: vehicle_model,
},
vehicleColumnSet,
'user_id'
);
await task.none(vehicleUpsertQuery);
PostgreSQL 在其多行插入语法中不支持任何 skip
逻辑。
And pg-promise documentation also tells you 内 skip
描述:
An override for skipping columns dynamically.
Used by methods update
(for a single object) and sets
, ignored by methods insert
and values
.
It is also ignored when conditional flag cnd
is set.
最多,您可以针对单行插入添加这样的逻辑,as shown here。
我无法弄清楚如何利用 skip
跳过 undefined/null 值。我不断收到 Error: Property 'vehicle_id' doesn't exist
。列集中的 skip
和 upsertReplaceQuery()
的 skip
是否相互冲突?我怎样才能让它工作?
const vehicleColumnSet = new pgp.helpers.ColumnSet(
[
{ name: 'user_id' },
{
name: 'vehicle_id',
skip: (c) => !c.exists,
},
{ name: 'model_id', def: null },
],
{ table: 'vehicle' }
);
const upsertReplaceQuery = (data, columnSet, conflictField) => {
return `${pgp.helpers.insert(
data,
columnSet
)} ON CONFLICT(${conflictField}) DO UPDATE SET ${columnSet.assignColumns({
from: 'EXCLUDED',
skip: conflictField,
})}`;
};
const vehicleUpsertQuery = upsertReplaceQuery(
{
user_id,
model_id: vehicle_model,
},
vehicleColumnSet,
'user_id'
);
await task.none(vehicleUpsertQuery);
PostgreSQL 在其多行插入语法中不支持任何 skip
逻辑。
And pg-promise documentation also tells you 内 skip
描述:
An override for skipping columns dynamically. Used by methods
update
(for a single object) andsets
, ignored by methodsinsert
andvalues
. It is also ignored when conditional flagcnd
is set.
最多,您可以针对单行插入添加这样的逻辑,as shown here。