Fix issue with auto-generated model ID during model creation
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -89,3 +90,15 @@ func (c *ModelParameters) Scan(value interface{}) error {
|
||||
}
|
||||
return json.Unmarshal(b, c)
|
||||
}
|
||||
|
||||
// BeforeCreate is a GORM hook that runs before creating a new model record
|
||||
// Automatically generates a UUID for new models
|
||||
// Parameters:
|
||||
// - tx: GORM database transaction
|
||||
//
|
||||
// Returns:
|
||||
// - error: Any error encountered during the hook execution
|
||||
func (m *Model) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
m.ID = uuid.New().String()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user