3 回答

TA贡献1906条经验 获得超3个赞
而不是将模型创建为嵌入式结构:
type MyModel struct {
gorm.Model
StringField string
IntField uint
}
您可以使用您在中提到的声明来创建它gorm.Model:
type MyModel struct {
ID uint `gorm:"primarykey"`
StringField string
IntField uint
}

TA贡献1802条经验 获得超4个赞
您可以避免 fields CreatedAt,UpdatedAt并且不在结构内部DeletedAt指定。gorm.Model
gorm:"column_name"您必须通过在它们旁边添加来显式声明这些结构字段在 Gorm 中的样子。
让我们假设您的遗留表被调用features并且它有列name和description。所以你的结构将是:
type Feature struct {
Name string `gorm:"column:name"`
Description string `gorm:"column:description"`
}
或者,如果此表除了 ORM 之外还用于 REST API:
type Feature struct {
Name string `json:"name" gorm:"column:name"`
Description string `json:"description" gorm:"column:description"`
}

TA贡献1815条经验 获得超6个赞
您可以通过将 autoUpdateTime 标记设置为 false 来禁用时间戳跟踪,例如:Source
CreatedAt time.Time `gorm:"autoCreateTime:false"`
UpdatedAt time.Time `gorm:"autoUpdateTime:false"`
- 3 回答
- 0 关注
- 721 浏览
添加回答
举报