19

我有以下型号...

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,但也没有运气。

4

6 回答 6

13

原来我试图将 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
}
于 2016-04-10T17:36:13.440 回答
4

为此,您将需要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:"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"`
}

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

于 2019-07-09T05:20:19.660 回答
2

对于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。
于 2021-07-27T04:37:18.070 回答
2

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

于 2021-07-13T23:35:01.963 回答
1

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

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

于 2016-04-08T01:51:28.580 回答
0

使用gorm v1.21,这些都不适合我。这是我的解决方案。请注意,我使用 satori/go.uuid 库来生成 UUID,但与谷歌库的代码几乎相同。

type UUIDBaseModel struct {
    ID        uuid.UUID       `gorm:"primary_key" json:"id"`
    CreatedAt time.Time  `json:"created_at"`
    UpdatedAt time.Time  `json:"updated_at"`
    DeletedAt *time.Time `sql:"index" json:"deleted_at"`
}

func (base *UUIDBaseModel) BeforeCreate(tx *gorm.DB) error {
    uuid := uuid.NewV4().String()
    tx.Statement.SetColumn("ID", uuid)
    return nil
}
于 2021-10-12T06:34:53.800 回答