Your Question
The docs says now it is able to Create() from map
db.Model(&User{}).Create(map[string]interface{}{"Name": "jinzhu", "Age": 18})
But how can I get LastInsertId? I try put an id key in the map, but after created, the id is still nil. So how can I get LastInsertId?
I know I can use User{} model to insert data and get LastInsertId, but there is some reason that I have to insert with a map, and get the LastInsertedId.
The document you expected this should be explained
Add an example of getting LastInsertId after create with map.
Expected answer
Get LastInsertId after Create() with map
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days
Comment From: yangwawa0323
When you create the User struct embedded the gorm.Model
type User struct{
gorm.Model
Name string
Age int
}
You would got a primary key ID by default. After the new user is committed by gorm, the user has been injected with ID, CreateAt, UpdateAt and DeleteAt attributes, So. You can change the code as following:
user := &User{"Name": "jinzhu", "Age": 18}
db.Create(user)
fmt.Printf("User id: %s", user.ID)