type BaseEntity struct {
ID string `gorm:"type:varchar(36);primary_key;comment:主键"`
CreateBy string `gorm:"type:varchar(64);default:system;comment:创建人"`
UpdateBy string `gorm:"type:varchar(64);default:system;comment:更新人"`
CreateTime *time.Time `gorm:"comment:创建时间;autoCreateTime"`
UpdateTime time.Time `gorm:"comment:更新时间;autoUpdateTime"`
}
func (b *BaseEntity) BeforeCreate(tx *gorm.DB) (err error) {
uuid := uuid.NewV4().String()
b.ID = strings.ReplaceAll(uuid, "-", "")
return
}
参考网络上的方案,自己就加了一个去掉中间 - 的代码
在多对多时,发现会不断的创建记录
后来发现将ID改成int后不会重复创建了
估计是创建的时候不断创建新的uuid了,于是加上了判断,问题解决...
到处百度都没找到答案,特地留下该解决方案
type BaseEntity struct {
ID string `gorm:"type:varchar(36);primary_key;comment:主键"`
CreateBy string `gorm:"type:varchar(64);default:system;comment:创建人"`
UpdateBy string `gorm:"type:varchar(64);default:system;comment:更新人"`
CreateTime *time.Time `gorm:"comment:创建时间;autoCreateTime"`
UpdateTime time.Time `gorm:"comment:更新时间;autoUpdateTime"`
}
func (b *BaseEntity) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == "" {
uuid := uuid.NewV4().String()
b.ID = strings.ReplaceAll(uuid, "-", "")
}
return
}