Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

软删除之后的行还可以被设置值 #5082

Closed
AnkoGo123 opened this issue Feb 17, 2022 · 10 comments
Closed

软删除之后的行还可以被设置值 #5082

AnkoGo123 opened this issue Feb 17, 2022 · 10 comments
Assignees

Comments

@AnkoGo123
Copy link

GORM Playground Link

go-gorm/playground#1

Description

软删除之后的行还可以通过以下的方式设置值 db.Model(b).Update("PId", newPId);
这是gorm没做限制吗?

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Feb 17, 2022
@github-actions
Copy link

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.ioSearch Before Asking

@li-jin-gou
Copy link
Member

正常是不会的,可以提个可复现的demo在playground上。

@github-actions
Copy link

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.ioSearch Before Asking

@li-jin-gou li-jin-gou removed the type:missing reproduction steps missing reproduction steps label Feb 18, 2022
@AnkoGo123
Copy link
Author

AnkoGo123 commented Feb 18, 2022

正常是不会的,可以提个可复现的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
}

如上代码,先软删除一行,然后对删除后的行的其他字段进行修改值还是可以修改的,如下图:
image

@AnkoGo123
Copy link
Author

1.22.4版本

@li-jin-gou
Copy link
Member

嗯....这个不完整....还是在 playground 放个复现的demo吧

@AnkoGo123
Copy link
Author

嗯....这个不完整....还是在 playground 放个复现的demo吧

你说的playground是哪里?

@AnkoGo123
Copy link
Author

嗯....这个不完整....还是在 playground 放个复现的demo吧

我觉得你们按照我上面的api来调用下即可复现,很容易复现我觉得,。。。

@AnkoGo123
Copy link
Author

AnkoGo123 commented Feb 18, 2022

问题已经被我解决。请看下面代码:

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,但是这个是非常的碍眼的东西

@li-jin-gou
Copy link
Member

@AnkoGo123 这个问题 在 #4897 被修复 可以更新 gorm 版本到 1.22.5

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants