3

我需要对参数化查询做些什么特别的事情吗?

以下似乎成功了(我正在使用promise-ified client.querysee end),

console.log('cancel for', data);
var cancelInviteQuery = 'delete from friendship where user_1=$1 and user_2_canonical=$2';
return query(cancelInviteQuery, [data.id, data.friend])
  .then(function (results) {
    console.log('ran delete frienship', results);
  })
  .catch(function (error) {
    console.error('failed to drop friendship', error);
  });

因为我得到了输出:

cancel for {"id":3,"friend":"m"}
ran delete frienship []

但是随后对数据库的以下查询显示该记录仍然存在

select * from friendship;
 id | user_1 | user_2 | user_2_canonical | confirmed | ignored 
----+--------+--------+------------------+-----------+---------
  8 |      3 |      9 | m                | f         | f

然后当我直接针对数据库进行以下查询时(使用 psql 客户端),以下查询成功

delete from friendship where user_1=3 and user_2_canonical='m'

我的query函数(node-postgres 的包装器client.query):

function query(sql, params) {
  var promise = new RSVP.Promise(function (resolve, reject) {
    pg.connect(CONNECTIONSTRING, function (error, client, done) {
      if (error) { reject(error); return; }

      client.query(sql, params, function (error, result) {
        done(); // Greedy close.
        if (error) {
          reject(error);
          return;
        }
        else if (result.hasOwnProperty('rows')) {
          resolve(result.rows);
        } else { resolve(result.rows); }
      });
    });
  });
  return promise;
}
4

1 回答 1

0

尝试这个

var cancelInviteQuery = 'delete from friendship where user_1 IN ($1) and user_2_canonical IN ($2)';
于 2016-05-07T10:46:29.133 回答