0

I am using gorm in my restful service, and I need to bring the deleted records. I can't see how to bring them and I can't see it in the documentation

thanks everyone, i update the controller working, Controller completed

func GetAllDeletedUsers(c *gin.Context) {
    var users []models.Application
    if err := db.DB.Unscoped().Where("deleted_at IS NOT NULL").Find(&users); err == nil {
        c.AbortWithStatus(404)
        fmt.Println(err)
    } else {
        c.JSON(200, users)
    }
}
4

2 回答 2

0

一行回复:

If you want to find users that not deleted. Just remove your Where() clause.

解释

在gorm中,如果您的模型中有gorm.Model,它会在您在表中查询时自动添加条件“(delete_at!= null)”。

如果您不希望发生这种情况,则应在 Delete() 之前添加 Unscope() 子句。

您可以在此处查看文档:在 Gorm 中删除

于 2020-12-03T15:32:06.720 回答
0

单行答案:

if err := db.DB.Unscoped().Where("deleted_at IS NOT NULL").Find(&users); err != nil {
    c.AbortWithStatus(404)
    fmt.Println(err)
} else {
    c.JSON(200, application)
}

文档链接:查找软删除记录

于 2020-12-03T16:44:19.737 回答