Your Question
An embedded structure cannot use db.Model to specify a model class to insert data
The document you expected this should be explained
The following is the code of reappearing
// models.User
type User struct {
ID uint `gorm:"primarykey" json:"id"`
Username string `json:"username"`
Password string `json:"-"`
}
type User struct {
ID uint `gorm:"primarykey" json:"id"`
Username string `json:"username"`
Password string `json:"-"`
}
type PostUserSchema struct {
models.User
Password string
}
schema := &PostUserSchema{
Password: "1",
User: models.User{
Username: "12",
},
}
tx := mysql.SqlDb.Table("user").Create(schema) // right
tx := mysql.SqlDb.Model(&models.User{}).Create(schema) // panic
error
2023-09-03T22:03:30.586+0800 ERROR runtime/panic.go:890 reflect: Field index out of range
runtime.gopanic
/home/x/software/go/src/runtime/panic.go:890
reflect.Value.Field
/home/x/software/go/src/reflect/value.go:1272
gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func1
/home/x/go/pkg/mod/gorm.io/gorm@v1.25.4/schema/field.go:446
gorm.io/gorm/callbacks.ConvertToCreateValues
/home/x/go/pkg/mod/gorm.io/gorm@v1.25.4/callbacks/create.go:270
gorm.io/gorm/callbacks.Create.func1
/home/x/go/pkg/mod/gorm.io/gorm@v1.25.4/callbacks/create.go:66
gorm.io/gorm.(*processor).Execute
/home/x/go/pkg/mod/gorm.io/gorm@v1.25.4/callbacks.go:130
gorm.io/gorm.(*DB).Create
/home/x/go/pkg/mod/gorm.io/gorm@v1.25.4/finisher_api.go:24
Expected answer
Can make Model(&models.User{}) work properly?
Comment From: saeidee
PostUserSchema & User are two different types and different fields.
Comment From: byte-voyager
But there is no error when use db.Model().Updates()