Answers for "gorm enum"

0

gorm enum

package model

import (
	"time"

	"github.com/google/uuid"
	"github.com/jinzhu/gorm"
)

type roleAccess string

const (
	admin    roleAccess = "admin"
	supplier roleAccess = "supplier"
)

type ModelRole struct {
	ID        string    `json:"id" gorm:"primary_key"`
	Type      string    `json:"type" gorm:"type:varchar"`
	Access    string    `json:"access" sql:"type:roleAccess"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

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

func (m *ModelRole) BeforeUpdate(db *gorm.DB) error {
	m.ID = uuid.NewString()
	m.UpdatedAt = time.Now()
	return nil
}
Posted by: Guest on October-19-2021
0

gorm enum

type carType string

const (
    SEDAN  carType = "SEDAN"
    HATCHBACK carType = "HATCHBACK"
    MINIVAN carType = "MINIVAN"
)

func (ct *carType) Scan(value interface{}) error {
    *ct = carType(value.([]byte))
    return nil
}

func (ct carType) Value() (driver.Value, error) {
    return string(ct), nil
}

type MyTable struct {
    gorm.Model
    CarType carType `sql:"car_type"`
}

func (MyTable) TableName() string {
    return "my_table"
}
Posted by: Guest on October-19-2021

Browse Popular Code Answers by Language