From ec6c71886ac203235ac93968c16ed5a169515711 Mon Sep 17 00:00:00 2001 From: Venus Date: Mon, 11 Dec 2023 11:00:02 +0800 Subject: [PATCH] Auto add alias for order column added via Column() method if the column in the table FieldMap --- query_select.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/query_select.go b/query_select.go index c0e145110..76ef8d44f 100644 --- a/query_select.go +++ b/query_select.go @@ -781,11 +781,22 @@ func (q *SelectQuery) appendOrder(fmter schema.Formatter, b []byte) (_ []byte, e if len(q.order) > 0 { b = append(b, " ORDER BY "...) - for i, f := range q.order { + for i, ord := range q.order { if i > 0 { b = append(b, ", "...) } - b, err = f.AppendQuery(fmter, b) + + // Auto add alias for order column added via Column() method if the column in the table FieldMap + if ord.Args == nil && q.table != nil { + if field, ok := q.table.FieldMap[ord.Query]; ok { + b = append(b, q.table.SQLAlias...) + b = append(b, '.') + b = append(b, field.SQLName...) + continue + } + } + + b, err = ord.AppendQuery(fmter, b) if err != nil { return nil, err }