GORM Playground Link
https://github.com/go-gorm/playground/pull/1
Description
软删除之后的行还可以通过以下的方式设置值 db.Model(b).Update("PId", newPId); 这是gorm没做限制吗?
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: li-jin-gou
正常是不会的,可以提个可复现的demo在playground上。
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: AnkoGo123
正常是不会的,可以提个可复现的demo在playground上。
// 单个删除
func DelPlayer(query interface{}, args ...interface{}) error {
result := DB.DBLink.Where(query, args).Delete(&Player{})
return result.Error
}
//改
//这里是更新多个列,但不是批量更新多行
func (p *Player) SetPasswordMD5(password string) error {
var cryptoStr = pwdEncode(password)
err := DB.DBLink.Model(p).Update("password", cryptoStr).Error
return err
}
如上代码,先软删除一行,然后对删除后的行的其他字段进行修改值还是可以修改的,如下图:
Comment From: AnkoGo123
1.22.4版本
Comment From: li-jin-gou
嗯....这个不完整....还是在 playground 放个复现的demo吧
Comment From: AnkoGo123
嗯....这个不完整....还是在 playground 放个复现的demo吧
你说的playground是哪里?
Comment From: AnkoGo123
嗯....这个不完整....还是在 playground 放个复现的demo吧
我觉得你们按照我上面的api来调用下即可复现,很容易复现我觉得,。。。
Comment From: AnkoGo123
问题已经被我解决。请看下面代码:
func TestSetAndGetPIdFriend(c *gin.Context) {
var p = Friend{
Model: gorm.Model{ID: 1},
}
fmt.Println("p:", p)
err := p.SetPIdFriend("299")
fmt.Println("p:", p)
if err != nil {
Result.NewResult(c).Error(404, err.Error())
} else {
nickName := p.GetPIdFriend()
fmt.Println("NickName:", nickName)
Result.NewResult(c).Success(map[string]interface{}{"data1": p})
}
}
func (b *Friend) SetPIdFriend(newPIdFriend string) error {
err := DB.DBLink.Model(b).Where("1=?", 1).Update("PIdFriend", newPIdFriend).Error//注意关注这行代码
return err
}
像上面这样会生成的sql语句是:UPDATE friends SET PIdFriend='299',updated_at='2022-02-18 20:14:14.524' WHERE 1=1 AND friends.deleted_at IS NULL AND id = 1
但是如果去除代码中的.Where("1=?", 1),则此时sql语句是: UPDATE friends SET PIdFriend='299',updated_at='2022-02-18 20:17:53.454' WHERE id = 1
通过对比上下2条sql语句可以很明显的发现只有在使用.where()方法来限制条件的情况下才会生成AND friends.deleted_at IS NULL AND id = 1,这是非常不合理的,望及时修复,我之前并未使用过gorm,我以为质量很好,通过使用我发现一些非常基础的bug都可以犯。。真的是很“国人”,之前我使用sqlx,现在看来这个orm并不怎么实用,目前我只能强迫通过添加.Where("1=?", 1)来修复你们的bug,但是这个是非常的碍眼的东西
Comment From: li-jin-gou
@AnkoGo123 这个问题 在 https://github.com/go-gorm/gorm/pull/4897 被修复 可以更新 gorm 版本到 1.22.5