binary(16)
Integrating GORM uuid implementation using new binary(16)
uuid implementation available in mysql version 8
.
BinaryUUID
uuid.UUID
inside and converts into binary when needed.Scan
and Value
for data type conversion.GormDataType
for data type in sql.MarshalJSON
and UnmarshalJSON
implementation for json encode/decode.BeforeCreate
BeforeCreate
hook for creating new uuids whenever new data has to be inserted.
// BeforeCreate ->
func (t *Test) BeforeCreate(tx *gorm.DB) error {
id, err := uuid.NewRandom()
t.ID = BinaryUUID(id)
return err
}
model := Test{}
db.Find(&model, "id = ?", ParseUUID("ed67a4b2-8f77-4d18-8c58-0508e7b207e8"))
log.Printf("data: %+v\n", model)
Will autogenerate uuid automatically because of BeforeCreate
hook added
data := Test{Name: fmt.Sprintf("Time is: %s", time.Now())}
db.Create(&data)
Check this article for more information