How do you do UUID in Golangs Gorm?

后端 未结 3 2155
闹比i
闹比i 2021-02-19 15:24

I have the following model...

type User struct {
    ID        string  `sql:\"type:uuid;primary_key;default:uuid_generate_v4()\"`
    FirstName string `form:\"f         


        
3条回答
  •  心在旅途
    2021-02-19 16:01

    Turns out I was trying to store the UUID as the wrong type, I was doing...

    func (user *User) BeforeCreate(scope *gorm.Scope) error {
        scope.SetColumn("ID", uuid.NewV4())
        return nil
    }
    

    When it needed to be...

    func (user *User) BeforeCreate(scope *gorm.Scope) error {
        scope.SetColumn("ID", uuid.NewV4().String())
        return nil
    }
    

提交回复
热议问题