gorm默认支持软删除
如果想要支持联合唯一索引,就需要用到一个gorm的插件库:gorm.io/plugin/soft_delete
By default, gorm.Model uses *time.Time as the value for the DeletedAt field, and it provides other data formats support with plugin gorm.io/plugin/soft_delete
INFO when creating unique composite index for the DeletedAt field, you must use other data format like unix second/flag with plugin gorm.io/plugin/soft_delete‘s help, e.g:
import "gorm.io/plugin/soft_delete"
type User struct {
ID uint
Name string `gorm:"uniqueIndex:udx_name"`
DeletedAt soft_delete.DeletedAt `gorm:"uniqueIndex:udx_name"`
}
这样的话,表users即支持了软删除,又支持了name和deleted_at的联合唯一索引,还是很方便的啊。
软删除和应删除操作
// 软删除
db.Where("id = 10").Delete(&User{})
// 查询的时候可以过滤掉软删除的数据
var u = make([]User, 0)
db.Find(&u)
fmt.Println(u)
// 硬删除:从数据库彻底删除数据
db.Unscoped().Where("id = ?", 7).Delete(&User{})
标签:删除,plugin,索引,io,soft,gorm,delete
From: https://www.cnblogs.com/mayanan/p/16662778.html