-
Notifications
You must be signed in to change notification settings - Fork 209
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
Postgres last insert id #29
Comments
Appending "RETURNING id" works if the table has the column id. If the driver has last insert id available, dbr will use it. Because https://golang.org/pkg/database/sql/driver/#RowsAffected.LastInsertId. I try to keep it simple, but all kinds of integer is possible. It will be the library users' responsibilities to ensure the id int types can hold the returned insert id. Do you have a specific use case for this? |
pq does not support LastInsertId(). You are correct, the column might not be named "id", but the name can be extracted from InsertBuilder.RecordID. |
You are welcomed to try this one: https://github.com/gocraft/dbr/pull/30/files. |
It looks kinda hacky and it does not support multiple records. This logic should be somewhere inside the dialect handler.
|
dbr tries to provide the minimal abstraction to hide database differences. Although batch auto-inject for postgres is easy with “RETURNING”, it is still hard for mysql (mysql, as far as I know, only has last insert id for this feature). There are a few obvious choices for dbr:
I prefer the second one. |
I think we'd love to have this work, but I don't think any of us have time right now to address it. If anybody has thoughts on how to best handle this I'd love to open up a discussion here. |
I've been poking around returning LastInsertID. Not sure how, but in some circumstances the LastInsertID works just fine (let's say >30%). At this point I don't see an easy way of getting an ID (it's not guaranteed that the ID even exist), thus one must use RETURNING column0 [, columnX]. I've hacked a bit around and added The branch is here: https://github.com/orian/dbr/tree/add-returning About using auto-inject: in many cases the dbr doesn't know primary key field and in some cases the primary key may not be a single column. It would cause the error to get returned. |
There is a way to do this in MySQL, but it has a couple of caveats. When inserting multiple rows, the value of LastInsertId will be the auto increment value of the first row inserted. The auto increment values for the other rows will be consecutive. So you can loop through the number of rows inserted and increment by the value of We are using this technique in a couple of places to implement bulk actions. Caveats:
|
I ran into this problem and although it is easily solved by just building your own SQL with the RETURNING keyword, I still wanted (and like) the InsertInto(...).Columns(...) syntax. So I have a diff that adds a Returning function on InsertBuilder struc : |
This issue is resolved with latest master. Thanks! |
Don't have any issues with the current implementation, but I wanted to add a quick code example because it took me a little while to piece this together. It looks like the var id int64
err := session.Conn().
InsertInto(<table-name>).
Columns(<column-names>).
Record(<struct-variable>).
Returning("id").
LoadContext(ctx, &id)
log.Printf("Last insert id is %d", id) |
https://github.com/gocraft/dbr/blob/master/example_test.go#L8 |
it's work, it's minimaize my code, thank you |
There does not seem to be a way of getting the record id of the last inserted record(s).
The correct way would be to append "RETURNING id" to the query, and then scan the ids, however, I don't see how that's possible currently.
Also, when using InsertBuilder.Record, an id field is only selected when its type is int64, why not allow any kind of integers?
The text was updated successfully, but these errors were encountered: