你如何在Golangs Gorm做UUID?

时间:2016-04-07 20:26:23

标签: 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
}

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

我尝试过使用...

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

与uuid lib一起,但也没有运气。

6 个答案:

答案 0 :(得分:8)

原来我试图将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
}

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

为此,您将需要gormgo.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:"update_at"`
 DeletedAt *time.Time `sql:"index" json:"deleted_at"`
}

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

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

因此,对于您的特殊情况,您将:

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"`
}

有关更多详细信息,请参见here

答案 3 :(得分:0)

提出的问题有点微妙,但对于那些来How do you do UUID in Golangs Gorm?...

使用 this link 获取最新版本的 gorm。

gorm.io/gorm v1.21.11 我得到了什么:

func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
  u.UUID = uuid.New()
  return
}

答案 4 :(得分:0)

对于 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 扩展,
    例如
    CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
  • 然后,当您调用 DB 的 Create() 方法时,会自动生成 uuid。

答案 5 :(得分:-1)

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

go get gorm.io/gorm
go get gorm.io/driver/postgres
go get github.com/google/uuid
import (
  "gorm.io/gorm"
  "github.com/google/uuid"
)

type User struct {
  Id: string `gorm:"primaryKey"`
}

// Note: Gorm will fail if the function signature
//  does not include `*gorm.DB` and `error`

func (user *User) BeforeCreate(tx *gorm.DB) (err error) {
  // UUID version 4
  user.Id = uuid.NewString()
  return
}

注意事项:

  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 包低 3.3 倍 (https://gist.github.com/mattes/69a4ab7027b9e8ee952b5843e7ca6955)

相关问题