简体   繁体   English

pg-promise helpers.update 不起作用

[英]pg-promise helpers.update not working

I am unable to get the helpers.update method working.我无法让helpers.update方法工作。 I am almost 100% certain that it is something simple I am overlooking.我几乎 100% 肯定这是我忽略的简单事情。 Below are the columns in the table involved in the code:以下是代码中涉及的表中的列:

  • user_social_profile_id
  • user_id
  • user_social_profile_platform
  • user_social_profile_link

Below is my definition for the ColumnSet .下面是我对ColumnSet定义。 (Note I added some spacing that is NOT in my actual code. I just hate having to scroll those windows.) (注意我添加了一些不在我的实际代码中的间距。我只是讨厌滚动这些窗口。)

const usersSocialProfiles = new pgp.helpers.ColumnSet(
  [ 
    'user_id', 
    'user_social_profile_platform',
    'user_social_profile_link'
  ],
  {table: 'users_social_profiles'}
);
const usersSocialProfilesUpdate = new pgp.helpers.ColumnSet(
  [ 
    '?user_social_profile_id',
    '?user_id',
    'user_social_profile_platform',
    'user_social_profile_link'
  ],
  {table:'users_social_profiles'}
);

Then a function to toggle between which ColumnSet I want to use然后是一个在我想使用的ColumnSet之间切换的函数

var pgpConstantSet = function(setName){
  if(setName === "usersSocialProfiles")
  {
    return usersSocialProfiles; 
  }
  if(setName === "updateUsersSocialProfiles")
  {
    return usersSocialProfilesUpdate;
  }
}

The attempt to use the ColumnSet尝试使用 ColumnSet

  var profiles = await pgp.helpers.update(userData.socialProfiles, 
  pgConstantSet("usersSocialProfiles"))+ 
  " WHERE t.user_social_profile_id=v.user_social_profile_id";
  console.log(profiles);

  return res.status(201).end();

And what the console.log(profiles) prints以及 console.log(profiles) 打印的内容

UPDATE "users_social_profiles" AS "t" SET "user_id"="v"."user_id","user_social_profile_platform"="v"."user_social_profile_platform","user_social_profile_link"="v"."user_social_profile_link" FROM (VALUES(51,'facebook','http:/w.sefvfaboklink.com')) AS "v"("user_id","user_social_profile_platform","user_social_profile_link") WHERE t.user_social_profile_id=v.user_social_profile_id PATCH /api/users 201 55.858 ms - -更新 "users_social_profiles" AS "t" SET "user_id"="v"."user_id","user_social_profile_platform"="v"."user_social_profile_platform","user_social_profile_link"="v"."user_social_profile_link" FROM (VALUES(51, 'facebook','http:/w.sefvfaboklink.com')) AS "v"("user_id","user_social_profile_platform","user_social_profile_link") WHERE t.user_social_profile_id=v.user_social_profile_id PATCH /api/users 201 55.858 ms ——

I should note that I used the other ColumnSet I defined for inserts worked like a charm.我应该注意到,我使用了我为插入定义的其他ColumnSet就像一个魅力。 I really appreciate anyone who got here.我真的很感谢来到这里的任何人。 I know this is a bit of a mess.我知道这有点乱。

All types and methods in the helpers namespace are for generating queries, which you confuse for executing them. helpers命名空间中的所有类型和方法都用于生成查询,您会因为执行它们而混淆。

In your code you only generate the query, but never execute it.在您的代码中,您只生成查询,但从不执行它。 You should remove the await before helpers.update call, as query generation is synchronous, and use one of Database methods to execute the query.您应该在helpers.update调用之前删除await ,因为查询生成是同步的,并使用数据库方法之一来执行查询。 The latter is asynchronous ;)后者是异步的;)


On another note, a better way for your example to declare usersSocialProfilesUpdate :另一方面,为您的示例声明usersSocialProfilesUpdate的更好方法是:

const usersSocialProfilesUpdate = usersSocialProfiles.merge([
    '?user_social_profile_id',
    '?user_id'
  ]);

See methods merge and extend that help avoiding re-declaration of the same columns.请参阅有助于避免重新声明相同列的方法合并扩展

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM