📜  gorm 枚举 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:20.699000             🧑  作者: Mango

代码示例2
type RoleAllowed string

const (
    admin    RoleAllowed = "admin"
    merchant RoleAllowed = "merchant"
    outlite  RoleAllowed = "outlite"
    supplier RoleAllowed = "supplier"
)

type ModelRole struct {
    ID         string         `json:"id" gorm:"primary_key"`
    RoleName   RoleAllowed    `json:"role_name" sql:"type:role_name"`
    RoleAccess pq.StringArray `json:"role_access" gorm:"type:text[]; not null"`
    CreatedAt  time.Time      `json:"created_at"`
    UpdatedAt  time.Time      `json:"updated_at"`
}

func (m *ModelRole) BeforeCreate(db *gorm.DB) error {
    m.ID = uuid.New().String()
    m.CreatedAt = time.Now().Local()
    return nil
}

func (m *ModelRole) BeforeUpdate(db *gorm.DB) error {
    m.ID = uuid.New().String()
    m.UpdatedAt = time.Now().Local()
    return nil
}