Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
johhansantana opened this issue Apr 17, 2017 · 6 comments
Closed

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

johhansantana opened this issue Apr 17, 2017 · 6 comments

Comments

@johhansantana
Copy link

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?

@brianc
Copy link
Owner

brianc commented Apr 17, 2017

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

@brianc brianc closed this as completed Apr 17, 2017
@jmarca
Copy link

jmarca commented 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', '[email protected]']

// 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 })
}

@charmander
Copy link
Collaborator

You don’t always need RETURNING and it’s something of a well-known PostgreSQL feature, so I’m not sure it has to be in driver docs.

@brianc
Copy link
Owner

brianc commented Aug 22, 2017

@jmarca I agree you don't always need a RETURNING on an insert, but I added a bit more docs based on your example since it couldn't hurt. 😄

https://node-postgres.com/features/queries#parameterized-query

@jmarca
Copy link

jmarca commented Aug 23, 2017

Yes of course you don't always need "returning", and postgresql inserts typically just return the sparse phrase INSERT 0 1 or whatnot. But its been a while, and the way the docs were written, I interpreted the res => console.log(res.rows[0]) as implying that res.rows[0] would contain the results of the insert. Of course it doesn't unless you ask for it, but I'd forgotten that.

That's the trouble with switching between different languages and databases...one remembers the broad stokes but forgets the details.

Thanks for the addition to the docs

@thdomingues-dev
Copy link

I'm using KnexJS and have the same problem, solve using "returning"

just like this: // Returns [1]
knex('books')
.returning('id')
.insert({title: 'Slaughterhouse Five'})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants