如何解决pg-promise 更新多个未正确设置的列 更新
我有一个项目需要一次更新多行。我发现有关如何执行此操作的示例是文档:documentation
我已经使用了列集,因为在文档中建议这样做。我已经设置了 ?feature_id
,所以它只用在 WHERE 子句中。
我的代码生成的错误如下:error: column "created_on" is of type timestamp with time zone but expression is of type text
。我在正在生成的查询中注意到,这似乎与示例一致。
这段代码有一个用于新功能的插入语句,并且似乎工作正常。该错误仅在更新查询中引发。
const insertValues = [];
const updateValues = [];
for (let i = 0; i < features.length; i += 1) {
const feature = features[i];
if (!excistingFeaturesIds.includes(feature.id)) {
insertValues.push({
plot_id: plotId,type: feature.type,area: feature.area,created_on: currendDate,updated_on: currendDate,geo_feature: feature.geoFeature,});
} else {
updateValues.push({
feature_id: feature.id,plot_id: plotId,});
}
}
const insertColumnSet = new pgp.helpers.ColumnSet(['plot_id','type','area','created_on','updated_on','geo_feature'],{ table: 'features' });
const updateColumnSet = new pgp.helpers.ColumnSet(['?feature_id','plot_id',{ table: 'features' });
if (insertValues && insertValues.length > 0) {
const insertQuery = pgp.helpers.insert(
insertValues,insertColumnSet,);
await promiseDB.none(insertQuery);
}
if (updateValues && updateValues.length > 0) {
const updateQuery = `${pgp.helpers.update(
updateValues,updateColumnSet,)} WHERE v.feature_id = t.feature_id`;
console.log(updateQuery);
await promiseDB.none(updateQuery);
}
return res.status(201).json({
message: 'Features added!',});
} catch (err) {
console.log(err);
return res.status(400).send(err);
}
UPDATE
"features" AS t
SET
"plot_id" = v. "plot_id","type" = v. "type","area" = v. "area","created_on" = v. "created_on","updated_on" = v. "updated_on","geo_feature" = v. "geo_feature"
FROM (
values(1,3,'roof',342.01520314642977,'2021-07-20T09:56:10.007+02:00','{"type":"Feature","geometry":{"type":"polygon","coordinates":[[coords...]]]},"properties":{"UIDN":6338864,"OIDN":5290477,"VERSIE":1,"BEGINDATUM":"2015-09-23","VERSDATUM":"2015-09-23","TYPE":1,"LBLTYPE":"hoofdgebouw","OPNDATUM":"2015-08-25","BGNINV":5,"LBLBGNINV":"kadastralisatie","type":"roof","tools":"polygon","description":"D1","id":5290477,"area":342.01520314642977,"roofType":"saddle","roofGreen":"normal","database":true},"id":1}'),(2,181.00725895629216,"coordinates":[[[coords...]]]},"properties":{"UIDN":6338518,"OIDN":5290131,"description":"D2","id":5290131,"area":181.00725895629216,"roofType":"flat","id":2}'),(3,24.450163203958745,"properties":{"UIDN":5473377,"OIDN":4708120,"BEGINDATUM":"2014-07-04","VERSDATUM":"2014-07-04","TYPE":2,"LBLTYPE":"bijgebouw","OPNDATUM":"2014-05-27","BGNINV":4,"LBLBGNINV":"bijhouding binnengebieden","description":"D3","id":4708120,"area":24.450163203958745,"id":3}'),(4,'water',57.65676046589426,"properties":{"UIDN":473256,"OIDN":199890,"VERSIE":2,"BEGINDATUM":"2017-03-08","VERSDATUM":"2021-05-06","VHAG":-9,"NAAM":"nvt","OPNDATUM":"2017-01-30","type":"water","description":"W1","id":199890,"area":57.65676046589426,"waterType":"natural","id":4}'))
AS v ("feature_id","plot_id","type","area","created_on","updated_on","geo_feature")
WHERE
v.feature_id = t.feature_id
INSERT INTO "features" ("plot_id","geo_feature")
values(3,'2021-07-20T10:17:04.565+02:00',"id":4}')
解决方法
您的列 created_on
和 updated_on
需要 SQL 类型转换,因此出现错误。
并且无需重新创建相同的表 -> 列列表。
总而言之,您的列集可以这样创建:
const insertColumnSet = new pgp.helpers.ColumnSet([
'plot_id','type','area',{name: 'created_on',cast: 'timestamptz'},{name: 'updated_on','geo_feature'
],{ table: 'features' });
const updateColumnSet = insertColumnSet.extend(['?feature_id']};
更新
请注意,严格来说,只有 UPDATE
需要类型转换,而您的 INSERT
可以自动推断类型,您可以像这样在列集中反映:
const insertColumnSet = new pgp.helpers.ColumnSet(['plot_id','created_on','updated_on','geo_feature'],{ table: 'features' });
const updateColumnSet = insertColumnSet.merge([
{name: 'feature_id',cnd: true},// or just '?feature_id'
{name: 'created_on',cast: 'timestamptz'}
]};
不过这两种方法在你的情况下都可以正常工作;)
参见 merge 方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。