Name mapping rules

2.1. Name mapping rules

Use names.Mapper interface to set the name mapping rule. There are three rules: SnakeMapper, SameMapper, and GonicMapper.

  • SnakeMapper inserts an _ (underscore) between each word (Capital Case) to get the table name or column name.
  • SameMapper uses the same name between the struct and table.
  • GonicMapper is basically the same as SnakeMapper, but doesn’t insert an underscore between commonly used acronyms. For example ID is converted to id in GonicMapper, but ID is converted to i_d in SnakeMapper.

SnakeMapper is the default. You can change it use SetMapper.

engine.SetMapper(names.SameMapper{})

And you should notice:

  • If you want to use other mapping rule, implement IMapper
  • Tables’s mapping rule could be different from Columns':
engine.SetTableMapper(names.SameMapper{})
engine.SetColumnMapper(names.SnakeMapper{})

When a struct auto mapping to a database’s table, the below table describes how they change to each other:

go type's kindvalue methodxorm type
implemented ConversionConversion.ToDB / Conversion.FromDBText
int, int8, int16, int32, uint, uint8, uint16, uint32Int
int64, uint64BigInt
float32Float
float64Double
complex64, complex128json.Marshal / json.UnMarshalVarchar(64)
[]uint8Blob
array, slice, map except []uint8json.Marshal / json.UnMarshalText
bool1 or 0Bool
stringVarchar(255)
time.TimeDateTime
cascade structprimary key field valueBigInt
structjson.Marshal / json.UnMarshalText
OthersText