新人使用Gorm的踩坑总结
在使用Update更新数据时一定要将where条件放在update前面,否则where不会生效,将更新所有数据
正确的写法
// 条件更新
db.Model(&User{}).Where("id= ?", ID).Update("name", "hello")
错误的写法
// 条件更新
db.Model(&User{}).Update("name", "hello").Where("id= ?", ID)
正确的写法
// 条件更新
db.Model(&User{}).Where("id= ?", ID).Update("name", "hello")
错误的写法
// 条件更新
db.Model(&User{}).Update("name", "hello").Where("id= ?", ID)