亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Gorm 獲取列名

Gorm 獲取列名

Go
HUWWW 2023-01-03 15:40:26
我在 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,->"`}是否可以獲取列名?我想要 gorm 將生成的列名
查看完整描述

2 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

可能的解決方案

解決方案是從模型中檢索(?解析?)模式。
請注意:來自模型——而不是來自“物理”數據庫。

參考

示例程序草稿

go.mod

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

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)

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

輸出

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


查看完整回答
反對 回復 2023-01-03
?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

使用這樣的標簽:


gorm:“列:uid”


type UserInfo struct {

    Uid       int       `json:"uid" gorm:"column:uid" form:"uid"` 

    Uname     string    `json:"uname" gorm:"column:uname" form:"uname"`

}


查看完整回答
反對 回復 2023-01-03
  • 2 回答
  • 0 關注
  • 294 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號