GORM的model屬性名稱(field name)與資料表欄位名稱(column name)的映射。
GORM慣例是model的屬性CamelCase名稱對應到資料表欄位snake_case名稱,例如model屬性CreatedAt對應到資料表欄位created_at。
若不符以上慣例,可使用Field Tags的column tag設定對應的欄位名稱。例如下面設定model Employee.CreatedAt屬性column tag `gorm:"column:created_time"`來對應到資料表employee.created_time欄位。
type Employee struct {
ID int64 // column name is `id`
Name string // column name is `name`
Age int // column name is `age`
CreatedAt time.Time `gorm:"column:created_time"` // column name is `created_time`
}
func (emp Employee) TableName() string {
return "employee" // 設定Employee對應資料表名稱為"employee"
}
沒有留言:
張貼留言