Your Question

For some special reasons, I need to query count() on the query with preload. What should I do? My idea is to make a deep copy of qurey and set the statement.Preloads of the copy to nil

func Paginate(c *gin.Context, originDb *gorm.DB) {
    countQuery := originDb
    if len(originDb.Statement.Preloads) > 0 {
        countQuery.Statement.Preloads = nil
    }
    var totalCount int64
    err := countQuery.Count(&totalCount).Error
}

After this modification, the preloads of originDb are also removed, but I want to keep it. Modifying the preloads of countQuery does not affect originDb. What should I do?

The document you expected this should be explained

Expected answer

Make a copy of originDb, modify the preloads of the copy but not affect originDb

Comment From: sunzhongwei

same issue