为了账号安全,请及时绑定邮箱和手机立即绑定

你如何在 Golangs Gorm 中做 UUID?

你如何在 Golangs Gorm 中做 UUID?

Go
喵喔喔 2022-01-04 10:05:08
我有以下型号...type User struct {    ID        string  `sql:"type:uuid;primary_key;default:uuid_generate_v4()"`    FirstName string `form:"first_name" json:"first_name,omitempty"`    LastName  string `form:"last_name" json:"last_name,omitempty"`    Password  string `form:"password" json:"password" bindind:"required"`    Email     string `gorm:"type:varchar(110);unique_index" form:"email" json:"email,omitempty" binding:"required"`    Location  string `form:"location" json:"location,omitempty"`    Avatar    string `form:"avatar" json:"avatar,omitempty"`    BgImg     string `form:"bg_img" json:"bg_img,omitempty"`    CreatedAt time.Time    UpdatedAt time.Time    DeletedAt time.Time}我尝试了几种不同的方法,但是这种方法会抛出(pq: relation "users" does not exist). 我没有相关的模型,它实际上只是一个模型。我试过用...func (user *User) BeforeCreate(scope *gorm.Scope) error {    scope.SetColumn("ID", uuid.NewV4())    return nil}与 uuid lib 一起,但也没有运气。
查看完整描述

3 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

原来我试图将 UUID 存储为错误的类型,我在做...


func (user *User) BeforeCreate(scope *gorm.Scope) error {

    scope.SetColumn("ID", uuid.NewV4())

    return nil

}

当它需要...


func (user *User) BeforeCreate(scope *gorm.Scope) error {

    scope.SetColumn("ID", uuid.NewV4().String())

    return nil

}


查看完整回答
反对 回复 2022-01-04
?
慕莱坞森

TA贡献1810条经验 获得超4个赞

为此,您将需要gorm和go.uuid


go get github.com/jinzhu/gorm


go get github.com/satori/go.uuid

尝试创建您自己的模型基础模型,而不是gorm.Model像这样:


type Base struct {

 ID         string     `sql:"type:uuid;primary_key;default:uuid_generate_v4()"`

 CreatedAt  time.Time  `json:"created_at"`

 UpdatedAt  time.Time  `json:"updated_at"`

 DeletedAt  *time.Time `sql:"index" json:"deleted_at"`

}

然后,您将使用在创建任何记录之前调用的方法填充此字段,如下所示:


func (base *Base) BeforeCreate(scope *gorm.Scope) error {

 id, err := uuid.NewV4()

 if err != nil {

   return err

 }

 return scope.SetColumn("ID", uuid.String())

}

因此,对于您的特定情况,您将拥有:


type User struct {

    Base

    FirstName string `form:"first_name" json:"first_name,omitempty"`

    LastName  string `form:"last_name" json:"last_name,omitempty"`

    Password  string `form:"password" json:"password" bindind:"required"`

    Email     string `gorm:"type:varchar(110);unique_index" form:"email" json:"email,omitempty" binding:"required"`

    Location  string `form:"location" json:"location,omitempty"`

    Avatar    string `form:"avatar" json:"avatar,omitempty"`

    BgImg     string `form:"bg_img" json:"bg_img,omitempty"`

}

可以在此处找到有关此的更多详细信息


查看完整回答
反对 回复 2022-01-04
?
长风秋雁

TA贡献1757条经验 获得超7个赞

对于postgresql,这是我所做的:

  • go get github.com/google/uuid

  • 使用uuid.UUID(来自“github.com/google/uuid”)作为类型,
    例如

    ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
  • 为 postgres 数据库添加 uuid-ossp 扩展,
    例如

    如果不存在则创建扩展“uuid-ossp”;
  • 然后,当您调用 DB 的 Create() 方法时,会自动生成 uuid。


查看完整回答
反对 回复 2022-01-04
  • 3 回答
  • 0 关注
  • 1272 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号