我在gorm中有以下模型

type Person struct {
    ID        uuid.UUID      `gorm:"type:uuid;default:uuid_generate_v4()"`
    Name      string         `gorm:"not null,type:text"`
    CreatedAt time.Time      `gorm:"autoCreateTime"`
    UpdatedAt time.Time      `gorm:"autoUpdateTime"`
    DeletedAt gorm.DeletedAt `gorm:"index,->"`
}

有可能拿到column name英镑吗?我想要gorm将生成的列名

推荐答案

Possible solution

The solution is to retrieve («parse») the schema from the model.
Please, note: from the model — not from a «physical» database.

工具书类

起草示 routine 序

go 摩登派青年

module gorm/example

go 1.18

require (
    github.com/google/uuid v1.3.0
    gorm.io/gorm v1.23.8
)

require (
    github.com/jinzhu/inflection v1.0.0 // indirect
    github.com/jinzhu/now v1.1.4 // indirect
)

go 总和

github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go 摩登派青年 h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go 摩登派青年 h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
github.com/jinzhu/now v1.1.4/go 摩登派青年 h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
gorm.io/gorm v1.23.8/go 摩登派青年 h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=

Program (main.go)

package main

import (
    "fmt"
    "github.com/google/uuid"
    "gorm.io/gorm"
    "gorm.io/gorm/schema"
    "sync"
    "time"
)

type Person struct {
    ID        uuid.UUID      `gorm:"type:uuid;default:uuid_generate_v4()"`
    Name      string         `gorm:"not null,type:text"`
    CreatedAt time.Time      `gorm:"autoCreateTime"`
    UpdatedAt time.Time      `gorm:"autoUpdateTime"`
    DeletedAt gorm.DeletedAt `gorm:"index,->"`
}

func main() {
    s, err := schema.Parse(&Person{}, &sync.Map{}, schema.NamingStrategy{})
    if err != nil {
        panic("failed to parse schema")
    }

    m := make(map[string]string)
    for _, field := range s.Fields {
        dbName := field.DBName
        modelName := field.Name
        m[modelName] = dbName
    }

    fmt.Println("Model to schema field name map:", m)
    fmt.Println("CreatedAt field is mapped to", m["CreatedAt"], "column")
}

建筑

$ go build main.go

$ ./main

Output

Model to schema field name map: map[CreatedAt:created_at DeletedAt:deleted_at ID:id Name:name UpdatedAt:updated_at]
CreatedAt field is mapped to created_at column

Go相关问答推荐

Go -SDP服务器读缓冲区不会更改任何内容

我不能让GIO画一个按钮

如何使用我的 struct 化日志(log)记录格式使人panic ?

将类型定义为泛型类型实例化

从使用Golang otelmux检测的Otel跟踪中获取trace_id

Go安装成功但没有输出简单的Hello World

Go:如何在不加载正文的情况下创建 http 代理通行证

在 GoLang 中对自定义 struct 体数组进行排序

从 ApiGateway 中的 lambda Go 返回 Json

使用Goldmark在golang中添加ChildNode会导致堆栈溢出

将字符串格式的x509证书生成主题名称

错误!在为 age-viewer-go 运行 wails dev 或 wails build 命令时

我突然无法再将我的 GoLang 应用程序部署到 Google AppEngine

GoLang: gocui 边框 colored颜色

在 Go 模板中对照片使用随机 Int

Golang - 客户 Unmarshaler/Marshaler 在指针上具有 nil/null 值

是否可以使用按位运算在随机 unicode 字符串中找到重复字符?

如何在 Unmarshal 中使用泛型(转到 1.18)

Scanner.Buffer - 最大值对自定义拆分没有影响?

Go 泛型是否与 LINQ to Objects 等效?