3 回答

TA貢獻1836條經驗 獲得超5個贊
嵌入 gorm 總是更好。默認情況下提供字段的結構中的模型:ID、創建時、更新時、已刪除。默認情況下,ID 將是主鍵,并且它是自動遞增的(由 GORM 管理)
type MyStructure struct {
gorm.Model
SomeFlag bool `gorm:"not null"`
Name string `gorm:"type:varchar(60)"`
}
刪除現有表:并再次創建表:,然后嘗試插入記錄。db.Migrator().DropTable(&MyStructure{})db.AutoMigrate(&MyStructure{})

TA貢獻1809條經驗 獲得超8個贊
只需更換您的結構
type MyStructure struct {
ID int32 `gorm:"primaryKey;autoIncrement:true"`
SomeFlag bool `gorm:"not null"`
Name string `gorm:"type:varchar(60)"`
}
與此
type MyStructure struct {
ID int32 `gorm:"AUTO_INCREMENT;PRIMARY_KEY;not null"`
SomeFlag bool `gorm:"not null"`
Name string `gorm:"type:varchar(60)"`
}
- 3 回答
- 0 關注
- 157 瀏覽
添加回答
舉報