-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
map insert support return increment id #6662
Conversation
callbacks/create.go
Outdated
if db.Statement.Schema.PrioritizedPrimaryField == nil || !db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue { | ||
return | ||
} | ||
|
||
insertID, err := result.LastInsertId() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some cases, the Dest
might be a map value, but db.Statement.Schema
is not nil, for example:
db.Model(&User{}).First(map[string]interface{}{})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, can you add some test cases for the feature, thank you.
65f2f63
to
54f0b22
Compare
fa6a876
to
98fb23f
Compare
98fb23f
to
e7301c1
Compare
e24d34f
to
a26ce78
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the detection of RETURNING
does not support map, an error will occur if the driver only supports returning but not LastInsertId (the database usually only supports one).
We need to change the detection of RETURNING
if we want to support this feat.
related to #6812
This PR creates bug for inserting rows (slice of map) in batches for Postgres Error: |
Sorry, I’ll check it and fix it with a new PR. |
I also cannot update gorm because this breaks inserting a map of values in postgres. Thanks for working on a fix! |
For the time being, till the issue is fixed, I've been checking for the error message. |
if I use createForUpdate logic,and the same logic is executed simultaneously in multiple databases,if not set Model, just know the table name. the same map will add “@id”,causing some SQL statements to fail execution. For example:
` |
Do only one thing
Non breaking API changes
Tested
What did this pull request do?
This pull request aims to support returning the primary key value, when using a map (or map list) to call GORM to insert record, in auto-increment primary key case.
If the primary key value is not set, the returned map or map list will be filled with a custom column "@id".
User Case Description
The following cases are all using auto-increment primary key
Case 1:
Input: map or map list without primary key
Output: map or map list will be filled with "@id"
Case 2:
Input: map or map list with primary key
Output: map or map list will be filled with "@id"
The following cases are all using non-auto-increment primary key
Case 1:
Input: map or map list without primary key
Output: error, the same as before.
Case 2:
Input: map or map list with primary key
Output: the same as before
Last but not least, it has no impact on non-auto-increment primary keys.