私はこれをテストしていないことを認め、私は以前はpgを使ったことがないと認めて始めます。
私がドキュメントから収集したことは、クエリオブジェクトが結果オブジェクトを持つイベント 'row'と 'end'を放出することです。私はこの結果オブジェクトにあなたのIDが含まれていると信じています。
var client = new Client('postgres://brian:[email protected]:5432/dev');
var query = client.query('INSERT INTO sometable VALUES ('foo', 'bar')');
query.on('row', function(row, result) {
// As I understand it, this will append all the affected rows to the
// result object so that they can be accessed in the 'end' event.
result.addRow(row);
});
query.on('end', function(result) {
// result.rows should contain the data you inserted along with the
// id that was generated.
console.log(result.rows.length + ' rows were received');
});
これは何らかの形で役立ちます。しかし、私はこのモジュールを使用する上での専門家であるとふりを言いません:) pg wikiはan example that shows how this is doneです。
//let's pretend we have a user table with the 'id' as the auto-incrementing primary key
var queryText = 'INSERT INTO users(password_hash, email) VALUES($1, $2) RETURNING id'
client.query(queryText, ['841l14yah', '[email protected]'], function(err, result) {
if(err) //handle error
else {
var newlyCreatedUserId = result.rows[0].id;
}
});
最初の調査では最初の手法を試しましたが、うまくいかなかった。しかし、 "復帰id"の部分は決して私には起こりませんでした。多くのありがとう:) – Faisal
編集された答えはとてもシンプルです –