-
Notifications
You must be signed in to change notification settings - Fork 911
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
not support LastInsertId() #24
Comments
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 Example:
More information here: |
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. |
Maybe we should panic in that method? On Monday, April 1, 2013, Isaiah Peng 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 |
Quite personally, I thought the returned error message was clever. It
|
hm yes, didn't realize it already returned an error. I agree that's enough, people should be checking those. |
A biblioteca de PostgreSQL utilizada não tem suporte ao Result.LastInsertId() [1], portanto vamos utilizar a sintaxe RETURNING conforme recomendado pela própria comunidade [2]. [1] https://golang.org/pkg/database/sql/#Result [2] lib/pq#24
A biblioteca de PostgreSQL utilizada não tem suporte ao Result.LastInsertId() [1], portanto vamos utilizar a sintaxe RETURNING conforme recomendado pela própria comunidade [2]. [1] https://golang.org/pkg/database/sql/#Result [2] lib/pq#24
How do I get the last ID from a prepared statement in a transaction?
Of course I'm getting: 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? |
QueryRow returns a result, and Exec does not. |
Maybe we should some more basics (or a link) to the docs? |
A PostgreSQL library does not support the result Result.LastInsertId()[1], here we will use a RETURNING syntax as recommended by own community [2]. [1] https://golang.org/pkg/database/sql/#Result [2] lib/pq#24
A PostgreSQL library does not support the result Result.LastInsertId()[1], here we will use a RETURNING syntax as recommended by own community [2]. [1] https://golang.org/pkg/database/sql/#Result [2] lib/pq#24
A PostgreSQL library does not support the result Result.LastInsertId()[1], here we will use a RETURNING syntax as recommended by own community [2]. [1] https://golang.org/pkg/database/sql/#Result [2] lib/pq#24
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 right way to solve this is to use the |
A PostgreSQL library does not support the result Result.LastInsertId()[1], here we will use a RETURNING syntax as recommended by own community [2]. [1] https://golang.org/pkg/database/sql/#Result [2] lib/pq#24
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. |
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. |
@jwatte to do that @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 |
@uhoh-itsmaciek Ah right you are - I opened a pull request. |
in the database/sql
the result has define
but the pg drive don't support LastInsertId()
The text was updated successfully, but these errors were encountered: