Name mapping rules
On this page
2.1. Name mapping rules
Use names.Mapper interface to set the name mapping rule. There are three rules: SnakeMapper, SameMapper, and GonicMapper.
SnakeMapperinserts an_(underscore) between each word (Capital Case) to get the table name or column name.SameMapperuses the same name between the struct and table.GonicMapperis basically the same as SnakeMapper, but doesn’t insert an underscore between commonly used acronyms. For exampleIDis converted toidin GonicMapper, butIDis converted toi_din 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 kind | value method | xorm type |
| implemented Conversion | Conversion.ToDB / Conversion.FromDB | Text |
| int, int8, int16, int32, uint, uint8, uint16, uint32 | Int | |
| int64, uint64 | BigInt | |
| float32 | Float | |
| float64 | Double | |
| complex64, complex128 | json.Marshal / json.UnMarshal | Varchar(64) |
| []uint8 | Blob | |
| array, slice, map except []uint8 | json.Marshal / json.UnMarshal | Text |
| bool | 1 or 0 | Bool |
| string | Varchar(255) | |
| time.Time | DateTime | |
| cascade struct | primary key field value | BigInt |
| struct | json.Marshal / json.UnMarshal | Text |
| Others | Text |