base.go 431 B

12345678910111213141516171819202122
  1. package models
  2. import (
  3. "time"
  4. "github.com/google/uuid"
  5. "gorm.io/gorm"
  6. )
  7. type Model struct {
  8. ID string `gorm:"column:id;primaryKey;type:uuid;default:uuid_generate_v4()" json:"id"`
  9. CreatedAt time.Time `json:"created_at"`
  10. UpdatedAt time.Time `json:"updated_at"`
  11. }
  12. func (m *Model) BeforeCreate(tx *gorm.DB) (err error) {
  13. if tx.Dialector.Name() == "mysql" && m.ID == "" {
  14. m.ID = uuid.New().String()
  15. }
  16. return
  17. }