From 300f399f4348b542bb47eabe553891a9201c99e7 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Wed, 18 Mar 2020 11:21:44 +0800 Subject: [PATCH] fix: list order by not effective --- pkg/cloudcommon/db/db_dispatcher.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/cloudcommon/db/db_dispatcher.go b/pkg/cloudcommon/db/db_dispatcher.go index 3d2b6f08ce..c3650e5b15 100644 --- a/pkg/cloudcommon/db/db_dispatcher.go +++ b/pkg/cloudcommon/db/db_dispatcher.go @@ -609,11 +609,22 @@ func ListItems(manager IModelManager, ctx context.Context, userCred mcclient.Tok } q, err = OrderByExtraFields(manager, ctx, q, userCred, orderQuery) if err != nil { - return nil, err + return nil, errors.Wrap(err, "OrderByExtraFields") } if orderBy == nil { orderBy = []string{} } + if primaryCol != nil && primaryCol.IsNumeric() { + orderBy = append(orderBy, primaryCol.Name()) + } else if manager.TableSpec().ColumnSpec("created_at") != nil { + orderBy = append(orderBy, "created_at") + if manager.TableSpec().ColumnSpec("name") != nil { + orderBy = append(orderBy, "name") + } + if primaryCol != nil { + orderBy = append(orderBy, primaryCol.Name()) + } + } for _, orderByField := range orderBy { if pagingConf != nil && utils.IsInStringArray(orderByField, pagingConf.MarkerFields) { // skip markerField in pagingConf @@ -628,17 +639,6 @@ func ListItems(manager IModelManager, ctx context.Context, userCred mcclient.Tok } } } - if primaryCol != nil && primaryCol.IsNumeric() { - orderBy = append(orderBy, primaryCol.Name()) - } else if manager.TableSpec().ColumnSpec("created_at") != nil { - orderBy = append(orderBy, "created_at") - if manager.TableSpec().ColumnSpec("name") != nil { - orderBy = append(orderBy, "name") - } - if primaryCol != nil { - orderBy = append(orderBy, primaryCol.Name()) - } - } if pagingConf != nil { q = q.Limit(int(limit) + 1)