From 2a85e371a26a061b17d230f9046cd15d0a1f35dd Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Thu, 8 May 2025 20:53:13 +0300 Subject: [PATCH] Fix GORM pagination with zero limit/offset Previously, q.Limit(0) was always set, causing GORM to return no records since limit=0 was interpreted as "get 0 records" rather than "no limit". Now we only set limit and offset when they're > 0, which makes pagination behave correctly with default values. --- engine/storage_sql.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 64a49a003..43f4c3d5e 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -218,8 +218,12 @@ func (sqls *SQLStorage) GetCDRs(ctx *context.Context, qryFltr []*Filter, opts ma if maxItems < limit+offset { return nil, fmt.Errorf("sum of limit and offset exceeds maxItems") } - q = q.Limit(limit) - q = q.Offset(offset) + if limit > 0 { + q = q.Limit(limit) + } + if offset > 0 { + q = q.Offset(offset) + } // Execute query results := make([]*utils.CDRSQLTable, 0)