我在 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,->"`
}
Run Code Online (Sandbox Code Playgroud)
是否可以获取列名?我想要 gorm 将生成的列名
解决方案是从模型中检索 (\xc2\xabparse\xc2\xbb) 架构。
\n请注意:来自模型 \xe2\x80\x94,而不是来自 \xc2\xabphysical\xc2\xbb 数据库。
如何获取结构/模型字段和数据库列的映射[字符串]字符串?\xc2\xb7 问题 #5114 \xc2\xb7 go-gorm/gorm。
\n稍微相关的问题。go - 如何从 gorm 中的模型获取表名?- 堆栈溢出。
\ngo.modmodule gorm/example\n\ngo 1.18\n\nrequire (\n github.com/google/uuid v1.3.0\n gorm.io/gorm v1.23.8\n)\n\nrequire (\n github.com/jinzhu/inflection v1.0.0 // indirect\n github.com/jinzhu/now v1.1.4 // indirect\n)\nRun Code Online (Sandbox Code Playgroud)\ngo.sumgithub.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=\ngithub.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=\ngithub.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=\ngithub.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=\ngithub.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=\ngithub.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=\ngorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=\ngorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=\nRun Code Online (Sandbox Code Playgroud)\nmain.go)module gorm/example\n\ngo 1.18\n\nrequire (\n github.com/google/uuid v1.3.0\n gorm.io/gorm v1.23.8\n)\n\nrequire (\n github.com/jinzhu/inflection v1.0.0 // indirect\n github.com/jinzhu/now v1.1.4 // indirect\n)\nRun Code Online (Sandbox Code Playgroud)\n$ go build main.go\nRun Code Online (Sandbox Code Playgroud)\n$ ./main\nRun Code Online (Sandbox Code Playgroud)\nModel to schema field name map: map[CreatedAt:created_at DeletedAt:deleted_at ID:id Name:name UpdatedAt:updated_at]\nCreatedAt field is mapped to created_at column\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3067 次 |
| 最近记录: |