Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

不支持 soft_delete 么? #41

Open
edocevol opened this issue Dec 2, 2024 · 5 comments
Open

不支持 soft_delete 么? #41

edocevol opened this issue Dec 2, 2024 · 5 comments
Assignees

Comments

@edocevol
Copy link

edocevol commented Dec 2, 2024

type Model struct {
	IncrID int64 `gorm:"column:incr_id;primaryKey;autoIncrement"`
	Version optimisticlock.Version
	DeletedAt soft_delete.DeletedAt
}

执行 delete 报错

if err := tx.Where("id = ?", m.ID).Delete(&entity.Model{}).Error; err != nil {
	return err
}

错误类似于

 converting driver.Value type clause.Expr ("{`version`+1 [] false}") to a int64: invalid syntax
@edocevol
Copy link
Author

edocevol commented Dec 2, 2024

当使用 update(&model) 的时候就会触发,这个问题需要怎么处理?

@icpd
Copy link
Collaborator

icpd commented Dec 2, 2024

@edocevol
Copy link
Author

edocevol commented Dec 6, 2024

Clipboard_Screenshot_1733457661

我这边测试发现,应该是

stmt.SetColumn(v.Field.DBName, clause.Expr{SQL: stmt.Quote(v.Field.DBName) + "+1"}, true)
stmt.Clauses["version_enabled"] = clause.Clause{}

成功之后,再走到 Scan 的逻辑的时候,出现的问题,这时候的的数据栈可以从图中看到,不是一个 int64 值(是我们设置的 +1 SQL )

@edocevol
Copy link
Author

edocevol commented Dec 6, 2024

update 之后,version 的值不会自动更新,需要再从数据库中查询一次

// Scan 实现 sql.Scanner 接口
func (v *OptimisticVersion) Scan(value interface{}) error {
	switch val := value.(type) {
	case int64:
		v.Int64 = val
		v.Valid = true
	case clause.Expr:
		return nil
	}
	return (*sql.NullInt64)(v).Scan(value)
}	

@icpd
Copy link
Collaborator

icpd commented Dec 6, 2024

能给一个完整的复现代码吗?

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

No branches or pull requests

3 participants