go - gorm 不生成 created_at , deleted_at , updated_at 这些列

go-rookie

2018-06-04T14:21:07Z||2018-06-04T14:21:07Z

类型不继承gorm.Model就可以。其实gorm.Model没什么特殊的地方,就是帮你定义好一些你可能常用的DB字段:

// Model base model definition, including fields `ID`, `CreatedAt`, `UpdatedAt`, `DeletedAt`, which could be embedded in your models
//    type User struct {
//      gorm.Model
//    }
type Model struct {
    ID        uint `gorm:"primary_key"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time `sql:"index"`
}

如果不需要的话就不用,如果需要一部分,单独写就好:

type User struct {
    ID        uint `gorm:"primary_key"`
    CreatedAt time.Time
    Desc      string `gorm:"type:text"`
}