Any way to get rid of automated column snake case renaming in GORM?

Viewed 891

I'm planning to use GORM with an existing database, therefore I'm creating some models for it. However, I've got one problem - GORM automatically renames all column to lower snake case. I don't really need it, because the database I work with doesn't really use such names. I've found out, that I can use tag

`gorm:"column_name:`

In order to prevent my columns from being renamed. However, it doesn't really seem to be a viable solution for me, because I have a bunch of models with tons of columns. Is there any way to switch off this "renaming" policy from GORM, or to automatically add a tag to all of my models?

My models look like that:

type FOOD_DES struct {
NDB_NO string `gorm:"primary_key"`
FdGrp_Cd FD_GROUP
Long_Desc string
Shrt_Desc string
ComName string
ManufacName string
Survey string
Ref_desc string
Refuse float32
SciName string
N_Factor float32
Pro_Factor float32
Fat_Factor float32
CHO_Factor float32
}
1 Answers

From doc:

GORM allows users to change the naming conventions by overriding the default NamingStrategy which need to implements interface Namer

type Namer interface {
  TableName(table string) string
  ColumnName(table, column string) string
  JoinTableName(table string) string
  RelationshipFKName(Relationship) string
  CheckerName(table, column string) string
  IndexName(table, column string) string
}

So just implement interface Namer.

And in the old version, you can do like (Ref)

gorm.AddNamingStrategy(&gorm.NamingStrategy{
    DB: func(name string) string {
        return name
    },
    Table: func(name string) string {
        return name
    },
    Column: func(name string) string {
        return name
    },
})
Related