type CommonModel struct {
// ID UUID as database index
ID string `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
}
// Credential database model
type User struct {
CommonModel
AppName string
Type int8
Data datatypes.JSON
Card []Card `gorm:"foreignKey:UserID"`
}
type Card struct {
CommonModel
Name string
UserID string
Data datatypes.JSON
}
已经跳用了注册函数:
func registerCallback(db *gorm.DB) {
// auto add uuid
err := db.Callback().Create().Before("gorm:create").Register("uuid", func(db *gorm.DB) {
db.Statement.SetColumn("id", NewUUID())
})
if err != nil {
panic(err)
}
}
当User中含有多个Card的时候,只有第一个card会自动生成ID,其他的均为空 在调用Create(&user)的时候,使用的数据库为Postgres
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: a631807682
https://gorm.io/docs/create.html#Batch-Insert create multiple record hook methods only invoked once
func (u *CommonModel) BeforeCreate(tx *gorm.DB) (err error) {
u.ID = NewUUID()
return
}