你如何在Golangs Gorm做UUID?

Ewa*_*ine 9 postgresql go go-gorm

我有以下型号......

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
}
Run Code Online (Sandbox Code Playgroud)

我尝试了几种不同的方式,但这种方式会抛出(pq: relation "users" does not exist).我没有相关的模型,它只是一个模型.

我试过用...

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

随着一个uuid lib,但也没有运气.

Eri*_*ang 23

对于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就会自动生成。

更新:第14页+gen_random_uuid()

(正如Doron Segal评论中提到的)

pg 14 有内置函数gen_random_uuid()来生成 uuid v4,例如:

创建表:

创建表uuid_test(uid文本默认gen_random_uuid());

插入一行:

插入 uuid_test(uid) 值(默认);

然后uid列会自动生成。


类似的,在go中你可以使用该函数作为默认值,我认为,例如:

ID uuid.UUIDgorm:"type:uuid;default:gen_random_uuid()"


顺便说一句,该gen_random_uuid()功能现在支持uuid v4,要使用其他版本,您仍然需要uuid-ossp扩展。

  • 如果您使用 Postgresql 版本 > 14,则不需要使用任何扩展。只需使用 gen_random_uuid() (2认同)

Ewa*_*ine 9

结果我试图将UUID存储为错误的类型,我正在做...

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

什么时候需要...

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

  • 你应该使用`return scope.SetColumn("ID",uuid.NewV4().String())`而不是忽略`SetColumn的结果 (2认同)

tfm*_*gue 8

这是我对 Gorm v1.21 的解决方案

go get gorm.io/gorm
go get gorm.io/driver/postgres
go get github.com/google/uuid
Run Code Online (Sandbox Code Playgroud)
go get gorm.io/gorm
go get gorm.io/driver/postgres
go get github.com/google/uuid
Run Code Online (Sandbox Code Playgroud)

笔记:

  1. 对于Google UUID包,方法uuid.New()uuid.NewString()使用UUID版本4。文档中没有明确说明(http://pkg.go.dev/github.com/google/uuid),但是通过查看源代码,您可以看到这些是uuid.NewRandom()UUID 版本 4 的包装器。

  2. 虽然有些人推荐 Satori UUID 包 ( https://github.com/satori/go.uuid ),但基准测试显示它的性能比 Google UUID 包 ( https://gist.github.com/mattes/ ) 低 3.3 倍69a4ab7027b9e8ee952b5843e7ca6955 )


Pet*_*njo 5

为此,您需要gormgo.uuid

go get github.com/jinzhu/gorm

go get github.com/satori/go.uuid
Run Code Online (Sandbox Code Playgroud)

尝试创建您自己的模型基础模型来代替,如下所示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"`
}
Run Code Online (Sandbox Code Playgroud)

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

func (base *Base) BeforeCreate(scope *gorm.Scope) error {
 id, err := uuid.NewV4()
 if err != nil {
   return err
 }
 return scope.SetColumn("ID", uuid.String())
}
Run Code Online (Sandbox Code Playgroud)

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

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"`
}
Run Code Online (Sandbox Code Playgroud)

有关此内容的更多详细信息,请参见此处

  • `default:uuid_generate_v4()` 不会自动为我们生成一个 UUID 吗? (14认同)

des*_*rth 1

该错误(pq: relation "users" does not exist)通常意味着数据库中不存在该表。 users它与两个模型之间的关系无关。

所以基本上,您首先需要在数据库中创建表(或者按照@Apin 建议自动迁移数据库)。并尝试重新运行相同的代码。