亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

你如何在 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 關注
  • 1222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號