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 關注
- 700 瀏覽
添加回答
舉報