Postgres - pg-promise - 无法更新多行 - table "y" 缺少 FROM 子句条目

Postgres - pg-promise - Unable to update multi rows - Missing FROM-clause entry for table "y"

我有一个项目,我需要更新链接到绘图的项目的功能。 API 调用获取功能列表。我首先在数据库中进行搜索,以找到该特定情节的所有令人兴奋的 feature_id。

然后我更新所有包含在令人兴奋的 ID 数组中的地块,并插入所有不包含的地块。

插入部分没问题。但是我的更新有问题。如果我想更新,我总是会收到以下错误 error: missing FROM-clause entry for table "y"

    const values = [];
    for (let i = 0; i < features.length; i += 1) {
      const feature = features[i];

      values.push({
        feature_id: feature.id,
        plot_id: plotId,
        type: feature.type,
        area: feature.area,
        created_on: currendDate,
        updated_on: currendDate,
        geo_feature: feature.geoFeature,
      });
    }

    const insert = new pgp.helpers.ColumnSet(['plot_id', 'type', 'area', 'created_on', 'updated_on', 'geo_feature'], { table: 'features' });
    const update = new pgp.helpers.ColumnSet(['?feature_id', 'plot_id', 'type', 'area', 'created_on', 'updated_on', 'geo_feature'], { table: 'features' });

    const insertingFeatures = values.find(
      (value) => !excistingFeaturesIds.includes(value.feature_id),
    );
    const updatingFeatures = values.find(
      (value) => excistingFeaturesIds.includes(value.feature_id),
    );

    if (insertingFeatures) {
      const insertQuery = pgp.helpers.insert(
        insertingFeatures, insert,
      );

      await promiseDB.none(insertQuery);
    }

    if (updatingFeatures) {
      const updateQuery = `${pgp.helpers.update(
        updatingFeatures, update,
      )} WHERE v.feature_id = t.feature_id`;

      await promiseDB.none(updateQuery);
    }

这是 updateQuery 的值 它似乎没有 from 子句。我不知道为什么会这样(我已经从查询中删除了坐标):

update "features" set "plot_id"='3',"type"='roof',"area"=342.01520314642977,"created_on"='2021-07-14T13:21:25.746+02:00',"updated_on"='2021-07-14T13:21:25.746+02:00',"geo_feature"='{"type":"Feature","geometry":{"type":"Polygon","coordinates":[...]},"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}' WHERE v.feature_id = t.feature_id

您收到错误是因为您没有考虑单行和多行更新之间的 SQL 语法差异。 It is in the documentation.

您的 updatingFeatures 最终成为单个对象,因此生成了一个单行更新,一个没有任何 FROM 子句的更新,因为它不应该有,而且您然后向其附加无效的 WHERE 子句。

如果您希望生成通用的多行更新,请确保updatingFeatures 始终是一组对象,而不仅仅是一个对象。