首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gorm获取列名

Gorm获取列名
EN

Stack Overflow用户
提问于 2022-07-16 21:40:57
回答 2查看 671关注 0票数 5

我在gorm中有下面的模型

代码语言:javascript
复制
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,->"`
}

是否有可能获得列名?我想要gorm生成的列名

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-19 15:40:13

可能解决办法

解决方案是从模型中检索(解析)模式。

请注意:来自模型-而不是物理数据库。

参考文献

代码语言:javascript
复制
- The solution comment: [**tzinckgraf**](https://github.com/tzinckgraf) commented [on Mar 4](https://github.com/go-gorm/gorm/issues/5114#issuecomment-1059285907).

示例程序草案

go.mod

代码语言:javascript
复制
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.sum

代码语言:javascript
复制
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=

程序(main.go)

代码语言:javascript
复制
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")
}

构建

代码语言:javascript
复制
$ go build main.go

代码语言:javascript
复制
$ ./main

输出

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2022-07-20 02:53:26

使用这样的标签:

gorm:“列:uid”

代码语言:javascript
复制
type UserInfo struct {
    Uid       int       `json:"uid" gorm:"column:uid" form:"uid"` 
    Uname     string    `json:"uname" gorm:"column:uname" form:"uname"`
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73007826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档