Hi In this issue we see https://github.com/go-gorm/gorm/issues/4497 that we are receiving only the DB Columns, but to get them mapped...? By knowing the structure field name i need to convert to a DB Column Name.. is there an existing function to do that..?
package main
import (
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"time"
)
type Animal struct {
AnimalID int64 `gorm:"column:beast_id"`
Birthday time.Time `gorm:"column:day_of_the_beast"`
Age int64 `gorm:"column:age_of_the_beast"`
}
func main() {
dsn := "host=postgres user=postgres password=***** dbname=postgres port=5432"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&Animal{})
result, _ := db.Debug().Migrator().ColumnTypes(&Animal{})
for _, v := range result {
fmt.Println(v.Name())
}
}
2021/07/04 12:34:50 /go/pkg/mod/gorm.io/driver/postgres@v1.1.1-0.20210614014255-4c6dc88331ab/migrator.go:68
[0.472ms] [rows:-] SELECT CURRENT_DATABASE()
2021/07/04 12:34:50 /go/pkg/mod/gorm.io/driver/postgres@v1.1.1-0.20210614014255-4c6dc88331ab/migrator.go:302
[9.747ms] [rows:-] SELECT column_name, is_nullable, udt_name, character_maximum_length, numeric_precision, numeric_precision_radix, numeric_scale, datetime_precision FROM information_schema.columns WHERE table_catalog = 'postgres' AND table_schema = CURRENT_SCHEMA() AND table_name = 'animals'
beast_id
day_of_the_beast
age_of_the_beast
Originally posted by @s-takehana in https://github.com/go-gorm/gorm/issues/4497#issuecomment-873987116
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: tzinckgraf
You could try something like the following
package main
import (
"fmt"
"gorm.io/gorm/schema"
"sync"
"time"
)
type Animal struct {
AnimalID int64 `gorm:"column:beast_id"`
Birthday time.Time `gorm:"column:day_of_the_beast"`
Age int64 `gorm:"column:age_of_the_beast"`
}
func main() {
s, err := schema.Parse(&Animal{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
panic("failed to create schema")
}
m := make(map[string]string)
for _, field := range s.Fields {
dbName := field.DBName
modelName := field.Name
m[modelName] = dbName
}
fmt.Println(m)
}
Comment From: DrOctavius
Hi @tzinckgraf , thank you very much! Works perfectly for me! I'll now close the issue!
Comment From: lukescott
Is there a way to do this while re-using the existing Config.NamingStrategy and sync.Map from gorm.DB? I have functionality where I have access to gorm.DB, but I want to make sure the schema.NamingStrategy matches. Ideally not duplicating the work of the existing map.