sql - 获取刚刚添加的行的最佳方式。 Heroku + Node + Postgres

标签 sql postgresql node.js heroku express

获取刚刚添加的行的最佳方式是什么,我正在使用 Heroku、Node 和 Postgres 以及 Expressjs。我希望能够做这样的事情。

app.post( '/', function( req, res ){
      client.query("INSERT into ..", function( err, result ){
            res.send( result.id );
      });
});

理想情况下,回调将包含有关它刚刚输入的行的信息,但它的内容只是一个看起来像的对象

{ rows:[] }

有什么好的方法可以获取我刚刚添加的那一行,谢谢。

最佳答案

您可能想使用 INSERT ... RETURNING :

The optional RETURNING clause causes INSERT to compute and return value(s) based on each row actually inserted. This is primarily useful for obtaining values that were supplied by defaults, such as a serial sequence number. However, any expression using the table's columns is allowed. The syntax of the RETURNING list is identical to that of the output list of SELECT.

所以像这样:

client.query('insert into your_table (...) values (...) returning *', function(err, result) {
    // ...
});

应该让您在回调函数中获得新插入的行。

关于sql - 获取刚刚添加的行的最佳方式。 Heroku + Node + Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9026730/

相关文章:

SQL返回一个字段包含相同数据但其他字段包含不同数据的行

node.js - Node js中基于ZeroMQ曲线的安全性

javascript - 使用 extend 将对象添加到原型(prototype)

sql - 如果查询完成时出现错误,是否需要回滚?

mysql - 学习如何连接表,但收到错误消息 "Not unique table"

mysql - MySQL中如何使用时间值获取所有数据?

postgresql - Postgres 从一列中选择不同的值并插入到表中

python - Django 多处理数据库并发

database - 如何在 postgres COPY 批量插入期间增加主键?

node.js - 为什么 Appcelerator Titanium 终端与我的操作系统终端不同?