question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A way to get the id of inserted row as callback?

See original GitHub issue

For inserting a row, the callback just sends back this:

{
  "command": "INSERT",
  "rowCount": 1,
  "oid": 0,
  "rows": [],
  "fields": [],
  "_parsers": [],
  "RowCtor": null,
  "rowAsArray": false
}

Is there a way to get the id of the inserted row?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

82reactions
brianccommented, Apr 17, 2017

INSERT INTO foo (name) VALUES ('baz') RETURNING *

6reactions
jmarcacommented, Aug 21, 2017

Got to this the long way. Can you update the docs (specifically at https://node-postgres.com/features/queries#parameterized-query) —to use this syntax?

I’ve used RETURNING before when doing a data-modifying WITH clause, but I didn’t think to add it to a plain insert.

(also, my way of using it is with async/await…)

Suggestion:

const text = 'INSERT INTO users(name, email) VALUES($1, $2) RETURNING *'
const values = ['brianc', 'brian.m.carlson@gmail.com']

// callback
client.query(text, values, (err, res) => {
  if (err) {
    console.log(err.stack)
  } else {
    console.log(res.rows[0])
  }
})

// promise
client.query(text, values)
  .then(res => console.log(res.rows[0]))
  .catch(e => console.error(e.stack))

// async/await, pool

try {
    const pool = new Pool()
    const {rows} = await pool.query(text, values)
    console.log(rows[0])
}catch(e){
   console.error(e)
   setImmediate(() => { throw e })
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Get the id of the just-inserted row - Stack Overflow
The insert operation does not provide the inserted data, so there is no way to obtain it from the server on the same...
Read more >
Is there a way to find ID of inserted row if there is trigger after ...
If I do something like this: new_id = db.table1.insert(...) and there is a trigger that inserts row in another table (table2) based on...
Read more >
PHP MySQL Get Last Inserted ID - W3Schools
If we perform an INSERT or UPDATE on a table with an AUTO_INCREMENT field, we can get the ID of the last inserted/updated...
Read more >
Python Psycopg2 - Getting ID of row just inserted
In this article, we are going to see how to get the ID of the row just inserted using pyscopg2 in Python ...
Read more >
Active Record Callbacks - Ruby on Rails Guides
How to create callback methods that respond to events in the object life cycle. ... #<User id: nil> irb> User.first You have found...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found