You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Correct. Postgres does not automatically return the last insert id, because it would be wrong to assume you're always using a sequence. You need to use the RETURNING keyword in your insert to get this information from postgres.
Example:
var id int
err := db.QueryRow("INSERT INTO user (name) VALUES ('John') RETURNING id").Scan(&id)
if err != nil {
...
}
Reacted by mahsaghoreishi, Ruslan Khmeliuk, Greggory Rothmeier, Frank Wang, JormaSnow, Pete Schmitt, Sreejith R, Nathan Davies, Hiroaki Nakamura, Peter Kleissner and 165 more
Reacted by Cengizhan Anıl Onay
Reacted by sn0rk64, JBR, Aliy Khoshimov, Mahdi, Martin Yonatan Pasaribu and Jasur Yusupov
Reacted by 9CAT, ww-zulongc, Denis Koltsov, Tara Firoozian, Ryan Ackermann, Pedro Fadel, Surfingbird, Fawwaz Yusran, Sinésio Neto, Alexander and 11 more
Reacted by Takumi Nishimori, Denis Koltsov, Tara Firoozian, Andy0x58, Pedro Fadel, Surfingbird, manlio, Fawwaz Yusran, Anton Kobzev, sn0rk64 and 16 more
Reacted by sn0rk64, JBR, Aliy Khoshimov, Eugene, Jasur Yusupov and Karuppiah Natarajan
Could you please kindly put this on the home page? Took me some time to find this piece of valuable information, someone from mysql may also get similar issue.
Reacted by Tim Hutt, Wyatt Arent, James Kiefer, Dylvn, James Halsall, Kamil Dziedzic, lidawei, mozhata, Jasur Yusupov, Karuppiah Natarajan and 6 more
Could you please kindly put this on the home page? Took me some time to
find this piece of valuable information, someone from mysql may also get
similar issue.
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/24#issuecomment-15739654
.
I think a panic with a message about using RETURNING would be appropriate, but I'm not sure how that would affect other libraries which may try to use database/sql in a driver-agnostic fashion. Might be worth asking bradfitz on golang-dev
Quite personally, I thought the returned error message was clever. It
didn't take me long to figure out that it wasn't supported. I am compelled
to ask how OP is doing error handling?
On Apr 1, 2013 10:27 PM, "Kamil Kisiel" notifications@github.com wrote:
I think a panic with a message about using RETURNING would be appropriate,
but I'm not sure how that would affect other libraries which may try to use
database/sql in a driver-agnostic fashion. Might be worth asking bradfitz
on golang-dev
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/24#issuecomment-15752137
.
According to the INSERT documentation, the default behaviour is to return the oid and count if no returning clause is present? https://www.postgresql.org/docs/9.6/static/sql-insert.html. If this is so, it should be made available to sql.Result.
Using OIDs is discouraged in user tables, and users will not insert directly into system tables. Do you have a use case for returning OIDs? It seems like the behavior you propose, while technically correct, would be confusing at best.
Yes, I want to update a row, sometime after I insert it. The use case is logging HTTP requests. I insert the initial values, and after responding, I update the row with stuff like response code, bytes written etc.
@anacrolix but are you using OIDs for those IDs? That's the only ids that Postgres returns to the client on insert, but they're not something you should be using--from the official docs:
The use of OIDs in user tables is considered deprecated...
The right way to solve this is to use the RETURNING clause and specify what data you would like to receive about the inserted row.
You could update LastInsertId() to return the ID returned if the query has a RETURNING clause, else return -1 and an error. This would let people re-use code that uses LastInsertId() from other drivers, which is important from a compatibility point of view.
Reacted by pinzolo, soyking, Vladimir Balin, Famofo Oluwaseun, Rangel Ivanov, danields761, zyukin, Eddy Reyes, Azareal, Mathieu PATUREL and 19 more
That just sounds like I've done something wrong that means the driver couldn't get the last insert ID. It doesn't make it clear that it isn't supported at all.
@jwatte to do that pq would have to parse the query which introduces a ton of additional complexity and still doesn't handle cases like non-integer (e.g., uuid) identifiers.
Activity
bmizerany commentedon Jun 12, 2012
Correct. Postgres does not automatically return the last insert id, because it would be wrong to assume you're always using a sequence. You need to use the
RETURNINGkeyword in your insert to get this information from postgres.Example:
More information here:
http://www.postgresql.org/docs/8.2/static/sql-insert.html
isaiah commentedon Apr 1, 2013
Could you please kindly put this on the home page? Took me some time to find this piece of valuable information, someone from mysql may also get similar issue.
bmizerany commentedon Apr 2, 2013
Maybe we should panic in that method?
On Monday, April 1, 2013, Isaiah Peng wrote:
kisielk commentedon Apr 2, 2013
I think a panic with a message about using RETURNING would be appropriate, but I'm not sure how that would affect other libraries which may try to use database/sql in a driver-agnostic fashion. Might be worth asking bradfitz on golang-dev
jackman0 commentedon Apr 2, 2013
Quite personally, I thought the returned error message was clever. It
didn't take me long to figure out that it wasn't supported. I am compelled
to ask how OP is doing error handling?
On Apr 1, 2013 10:27 PM, "Kamil Kisiel" notifications@github.com wrote:
kisielk commentedon Apr 2, 2013
hm yes, didn't realize it already returned an error. I agree that's enough, people should be checking those.
New char type
https://github.com/lib/pq/issues/24
Corrige a estratégia de obtenção do ID
Corrige a estratégia de obtenção do ID
paulm17 commentedon Oct 23, 2016
How do I get the last ID from a prepared statement in a transaction?
Of course I'm getting:
panic: no LastInsertId available
Everywhere I read, like above is to use QueryRow.
Which I tried and works fine.
What's the difference between Exec and QueryRow for this driver then?
tamird commentedon Oct 23, 2016
QueryRow returns a result, and Exec does not.
cbandy commentedon Oct 23, 2016
Maybe we should some more basics (or a link) to the docs?
Add transactions in SQL operations
Add transactions in SQL operations
Add transactions in SQL operations
anacrolix commentedon May 29, 2017
According to the INSERT documentation, the default behaviour is to return the oid and count if no returning clause is present? https://www.postgresql.org/docs/9.6/static/sql-insert.html. If this is so, it should be made available to sql.Result.
msakrejda commentedon May 30, 2017
Using OIDs is discouraged in user tables, and users will not insert directly into system tables. Do you have a use case for returning OIDs? It seems like the behavior you propose, while technically correct, would be confusing at best.
anacrolix commentedon May 31, 2017
Yes, I want to update a row, sometime after I insert it. The use case is logging HTTP requests. I insert the initial values, and after responding, I update the row with stuff like response code, bytes written etc.
msakrejda commentedon May 31, 2017
@anacrolix but are you using OIDs for those IDs? That's the only ids that Postgres returns to the client on insert, but they're not something you should be using--from the official docs:
The right way to solve this is to use the
RETURNINGclause and specify what data you would like to receive about the inserted row.Add transactions in SQL operations
jwatte commentedon May 8, 2018
You could update LastInsertId() to return the ID returned if the query has a RETURNING clause, else return -1 and an error. This would let people re-use code that uses LastInsertId() from other drivers, which is important from a compatibility point of view.
Timmmm commentedon Sep 29, 2018
The error message for this is also not clear:
That just sounds like I've done something wrong that means the driver couldn't get the last insert ID. It doesn't make it clear that it isn't supported at all.
msakrejda commentedon Sep 29, 2018
@jwatte to do that
pqwould have to parse the query which introduces a ton of additional complexity and still doesn't handle cases like non-integer (e.g., uuid) identifiers.@Timmmm good point, but I think that message comes from https://github.com/golang/go/blob/50bd1c4d4eb4fac8ddeb5f063c099daccfb71b26/src/database/sql/driver/driver.go#L472 not
pq. Maybe file an issue upstream?Timmmm commentedon Oct 3, 2018
@uhoh-itsmaciek Ah right you are - I opened a pull request.
pdkovacs commentedon May 16, 2021
@tamird
As per specification, Exec should return a Result – unless an error occurs during execution.
database.go: postgres does not support LastInsertId(), use solution f…
fix(test): PostgreSQL does not support LastInsertId()
fix(test): use RETURNING clause for PostgreSQL insert ID