Closed
Description
GORM Playground Link
Description
I'm slightly changing the Create
example.
Changing ID
from uint
to string
make Create
not return the inserted ID
.
type User struct {
ID string
Name string
Age uint8
}
user := User{Name: "Jinzhu", Age: 18}
_ = db.Create(&user) // pass pointer of data to Create
This yields to: INSERT INTO "users" ("id","name","age") VALUES ('','Jinzhu',18)
To make it work, I have to "force" a wrong method for default
tag, like:
type User struct {
ID string `gorm:"default:whatever()"`
Name string
Age uint8
}
This yields correctly to INSERT INTO "users" ("name","age") VALUES ('Jinzhu',18) RETURNING "id"
I couldn't find a way to set the DEFAULT
keyword, as gorm will make it a string and try to insert that as "DEFAULT"
type User struct {
ID string `gorm:"default:DEFAULT"`
Name string
Age uint8
}
This yields to INSERT INTO "users" ("id","name","age") VALUES ('DEFAULT','Jinzhu',18)
This is a breaking change, as with v1 Create
would return the id
without problems