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

Save() 会覆盖软删除 #4894

Closed
Aoang opened this issue Dec 3, 2021 · 6 comments · Fixed by #4897
Closed

Save() 会覆盖软删除 #4894

Aoang opened this issue Dec 3, 2021 · 6 comments · Fixed by #4897
Assignees
Labels

Comments

@Aoang
Copy link
Contributor

Aoang commented Dec 3, 2021

描述

问题比较简单,数据库中某条记录被软删除,如果使用 Save() 保存数据,会恢复软删除。

场景出现在多协程中,某个协程删除了一条记录,另一个协程在没有使用事务的情况下(执行时间比较长)保存数据。

按照行为来定义的话,Save() 应该会报错,而不是直接保存。

Code

type Model struct {
	ID        uint64         `gorm:"primaryKey" json:"id" `
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}

type Port struct {
	Model

	Name     string    `json:"name"`
}


var db *gorm.DB

func main() {
	p := &Port{Name: "name"}
	if err := db.Create(p).Error; err != nil {
		fmt.Println(err)
	}
	if err := db.Delete(&Port{Model: Model{ID: p.ID}}).Error; err != nil {
		fmt.Println(err)
	}
	if err := db.Save(p).Error; err != nil {
		fmt.Println(err)
	}
	fmt.Println(p)
}

逻辑

  1. 创建一条记录
  2. 删除这条记录
  3. 使用 Save() 保存数据

预期:报错,提示数据不存在
结果:数据保存成功,且软删除无了

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Dec 3, 2021
@github-actions
Copy link

github-actions bot commented Dec 3, 2021

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

@ghost
Copy link

ghost commented Dec 3, 2021

https://gorm.cn/zh_CN/docs/update.html#%E4%BF%9D%E5%AD%98%E6%89%80%E6%9C%89%E5%AD%97%E6%AE%B5
Save 是保存数据,如果数据存在就会更新,不存在就会创建.传递的�P里面的Del的标示位还是未删除~

@github-actions
Copy link

github-actions bot commented Dec 3, 2021

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

@Aoang
Copy link
Contributor Author

Aoang commented Dec 3, 2021

https://gorm.cn/zh_CN/docs/update.html#%E4%BF%9D%E5%AD%98%E6%89%80%E6%9C%89%E5%AD%97%E6%AE%B5 Save 是保存数据,如果数据存在就会更新,不存在就会创建.传递的�P里面的Del的标示位还是未删除~

看了看 Save() 的行为,的确会在不存在的情况下创建。但是我说的问题依旧存在。


软删除的行为定义下,数据被软删除之后,ORM 应当认定为不存在此条数据,Save() 的行为应该是

  1. 尝试更新数据,找不到对应的记录
  2. 尝试创建记录,主键冲突

但目前的行为是

  1. 尝试更新数据,找到对应的被软删除的记录
  2. 更新记录。此时因为传递给 Save() 的参数中 DeletedAt 为 nil,导致软删除的记录被恢复了

如果 Save() 能在不覆盖 DeletedAt 的情况下更新数据倒是符合需求,但是不符合 Save() 的定义了。

@github-actions
Copy link

github-actions bot commented Dec 3, 2021

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

@ghost ghost linked a pull request Dec 3, 2021 that will close this issue
@github-actions github-actions bot closed this as completed Dec 5, 2021
@ghost
Copy link

ghost commented Dec 8, 2021

@Aoang already fix, thank you.

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

Successfully merging a pull request may close this issue.

2 participants