Your Question
Useing the soft delete, gorm can only update one column like deleted_at or flag.
But now, I alse need to update the deleted_by column. I want to record the user who operated this.
How can I do this?
Thank you.
The document you expected this should be explained
Expected answer
Comment From: jianjianxu
意思就是gorm执行软删除的时候,执行的是update set deleted_at=当前时间。
有没有什么方法,让调用delete操作的时候,最终执行的是
update set deleted_at=当前时间 and deleted_by=操作人id。
Comment From: jianjianxu
BeforeDelete、AfterDelete ,这倒是可以实现,但是我感觉这样做一个是得多好几行代码,再一个这样是不是会执行两个sql的更新,这样费连接
Comment From: xuxing421
Actually gorm soft delete is update operation, how about creating a function like this
func DeleteWithOperator(operator string) func(db gorm.DB) gorm.DB { // TODO }
Then you can use Scope call the function err := db.Where("id = ? ", teacherId).Scopes(DeleteWithOperator(xxx)).Error
Comment From: a631807682
You can achieve this through custom types
https://github.com/go-gorm/gorm/blob/master/soft_delete.go