not support LastInsertId() #24

Closed
astaxie opened this Issue Jun 12, 2012 · 9 comments

Projects

None yet

8 participants

@astaxie
astaxie commented Jun 12, 2012

in the database/sql
the result has define

type Result

type Result interface {
    LastInsertId() (int64, error)
    RowsAffected() (int64, error)
}

but the pg drive don't support LastInsertId()

@bmizerany
Contributor

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 {
...
}

More information here:
http://www.postgresql.org/docs/8.2/static/sql-insert.html

@bmizerany bmizerany closed this Jun 12, 2012
@isaiah
isaiah commented 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
Contributor

Maybe we should panic in that method?

On Monday, April 1, 2013, Isaiah Peng wrote:

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/lib/pq/issues/24#issuecomment-15739654
.

@kisielk
Contributor
kisielk commented 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

@jackmanlabs

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/lib/pq/issues/24#issuecomment-15752137
.

@kisielk
Contributor
kisielk commented Apr 2, 2013

hm yes, didn't realize it already returned an error. I agree that's enough, people should be checking those.

@thermokarst thermokarst pushed a commit to thermokarst/bactdb that referenced this issue Oct 29, 2015
Matthew Dillon New char type f2a1c7c
@yinshuwei yinshuwei added a commit to yinshuwei/osm that referenced this issue Apr 29, 2016
@yinshuwei yinshuwei lib/pq#24
pg lastInsertId
f44b13b
@rafaeljusto rafaeljusto added a commit to rafaeljusto/atiradorfrequente that referenced this issue Sep 20, 2016
@rafaeljusto rafaeljusto Corrige a estratégia de obtenção do ID
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
3b12f79
@rafaeljusto rafaeljusto added a commit to rafaeljusto/atiradorfrequente that referenced this issue Sep 20, 2016
@rafaeljusto rafaeljusto Corrige a estratégia de obtenção do ID
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
1258675
@no1youknowz
no1youknowz commented Oct 23, 2016 edited

How do I get the last ID from a prepared statement in a transaction?

sql := `
    insert into [table] (col1, col2, col3, col4, col5, col6)
    values ($1, $2, $3, $4, $5, $6) RETURNING id;
`
stmt, err := db.Prepare(sql)

if err != nil {
    panic(err)
}

res, err := stmt.Exec(data1, data2, data3, data4, data5, data6)

if err != nil {
    panic(err)
} else {
    ID, err = res.LastInsertId()

    if err != nil {
        panic(err)
    }
}

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
Collaborator
tamird commented Oct 23, 2016

QueryRow returns a result, and Exec does not.

@cbandy
Contributor
cbandy commented Oct 23, 2016

Maybe we should some more basics (or a link) to the docs?

@marioidival marioidival added a commit to nuveo/prest that referenced this issue Jan 5, 2017
@marioidival marioidival Add transactions in SQL operations
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
205ed66
@marioidival marioidival referenced this issue in nuveo/prest Jan 5, 2017
Merged

Add transactions in SQL operations #61

@marioidival marioidival added a commit to nuveo/prest that referenced this issue Jan 5, 2017
@marioidival marioidival Add transactions in SQL operations
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
db0b449
@marioidival marioidival added a commit to nuveo/prest that referenced this issue Jan 5, 2017
@marioidival marioidival Add transactions in SQL operations
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
97c252e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment