Gorm获取列名

huangapple go评论118阅读模式
英文:

Gorm get column name

问题

我在gorm中有以下模型:

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

是否可以获取列名?我想要gorm将生成的列名。

英文:

I have the following model in gorm

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

Is it possible to get the column name? I want the column name that gorm will generate

答案1

得分: 5

可能的解决方案

解决方案是从模型中检索("解析")模式。
请注意:是从模型中检索,而不是从"物理"数据库中检索。

参考资料

草稿示例程序

go.mod

  1. module gorm/example
  2. go 1.18
  3. require (
  4. github.com/google/uuid v1.3.0
  5. gorm.io/gorm v1.23.8
  6. )
  7. require (
  8. github.com/jinzhu/inflection v1.0.0 // indirect
  9. github.com/jinzhu/now v1.1.4 // indirect
  10. )

go.sum

  1. github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
  2. github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
  3. github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
  4. github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
  5. github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
  6. github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
  7. gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
  8. gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=

程序 (main.go)

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/google/uuid"
  5. "gorm.io/gorm"
  6. "gorm.io/gorm/schema"
  7. "sync"
  8. "time"
  9. )
  10. type Person struct {
  11. ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
  12. Name string `gorm:"not null,type:text"`
  13. CreatedAt time.Time `gorm:"autoCreateTime"`
  14. UpdatedAt time.Time `gorm:"autoUpdateTime"`
  15. DeletedAt gorm.DeletedAt `gorm:"index->"`
  16. }
  17. func main() {
  18. s, err := schema.Parse(&Person{}, &sync.Map{}, schema.NamingStrategy{})
  19. if err != nil {
  20. panic("failed to parse schema")
  21. }
  22. m := make(map[string]string)
  23. for _, field := range s.Fields {
  24. dbName := field.DBName
  25. modelName := field.Name
  26. m[modelName] = dbName
  27. }
  28. fmt.Println("Model to schema field name map:", m)
  29. fmt.Println("CreatedAt field is mapped to", m["CreatedAt"], "column")
  30. }

构建

  1. $ go build main.go

运行

  1. $ ./main

输出

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

Possible solution

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

References

Draft example program

go.mod

  1. module gorm/example
  2. go 1.18
  3. require (
  4. github.com/google/uuid v1.3.0
  5. gorm.io/gorm v1.23.8
  6. )
  7. require (
  8. github.com/jinzhu/inflection v1.0.0 // indirect
  9. github.com/jinzhu/now v1.1.4 // indirect
  10. )

go.sum

  1. github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
  2. github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
  3. github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
  4. github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
  5. github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
  6. github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
  7. gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
  8. gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=

Program (main.go)

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/google/uuid"
  5. "gorm.io/gorm"
  6. "gorm.io/gorm/schema"
  7. "sync"
  8. "time"
  9. )
  10. type Person struct {
  11. ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
  12. Name string `gorm:"not null,type:text"`
  13. CreatedAt time.Time `gorm:"autoCreateTime"`
  14. UpdatedAt time.Time `gorm:"autoUpdateTime"`
  15. DeletedAt gorm.DeletedAt `gorm:"index,->"`
  16. }
  17. func main() {
  18. s, err := schema.Parse(&Person{}, &sync.Map{}, schema.NamingStrategy{})
  19. if err != nil {
  20. panic("failed to parse schema")
  21. }
  22. m := make(map[string]string)
  23. for _, field := range s.Fields {
  24. dbName := field.DBName
  25. modelName := field.Name
  26. m[modelName] = dbName
  27. }
  28. fmt.Println("Model to schema field name map:", m)
  29. fmt.Println("CreatedAt field is mapped to", m["CreatedAt"], "column")
  30. }

Build

  1. $ go build main.go

Run

  1. $ ./main

Output

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

答案2

得分: 1

使用标签的方式如下:

> gorm:"column:uid"

  1. type UserInfo struct {
  2. Uid int `json:"uid" gorm:"column:uid" form:"uid"`
  3. Uname string `json:"uname" gorm:"column:uname" form:"uname"`
  4. }
英文:

use tag like this:

> gorm:"column:uid"

  1. type UserInfo struct {
  2. Uid int `json:"uid" gorm:"column:uid" form:"uid"`
  3. Uname string `json:"uname" gorm:"column:uname" form:"uname"`
  4. }

huangapple
  • 本文由 发表于 2022年7月17日 05:40:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/73007826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定