From 5c21f1c27026acca298ea5f6dcbf00a3672b02c3 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Thu, 9 Sep 2021 17:09:48 +0300 Subject: [PATCH] fix: check if table is present when appending columns --- internal/dbtest/db_test.go | 14 +++++++++++--- query_select.go | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/dbtest/db_test.go b/internal/dbtest/db_test.go index de3b9b097..4067c7a3a 100644 --- a/internal/dbtest/db_test.go +++ b/internal/dbtest/db_test.go @@ -265,6 +265,7 @@ func testSelectScan(t *testing.T, db *bun.DB) { func testSelectCount(t *testing.T, db *bun.DB) { if !db.Dialect().Features().Has(feature.CTE) { t.Skip() + return } values := db.NewValues(&[]map[string]interface{}{ @@ -273,12 +274,19 @@ func testSelectCount(t *testing.T, db *bun.DB) { {"num": 3}, }) - count, err := db.NewSelect(). + q := db.NewSelect(). With("t", values). + Column("t.num"). TableExpr("t"). OrderExpr("t.num DESC"). - Limit(1). - Count(ctx) + Limit(1) + + var num int + err := q.Scan(ctx, &num) + require.NoError(t, err) + require.Equal(t, 3, num) + + count, err := q.Count(ctx) require.NoError(t, err) require.Equal(t, 3, count) } diff --git a/query_select.go b/query_select.go index 7ff93366f..418bc04ce 100644 --- a/query_select.go +++ b/query_select.go @@ -523,7 +523,7 @@ func (q *SelectQuery) appendColumns(fmter schema.Formatter, b []byte) (_ []byte, b = append(b, ", "...) } - if col.Args == nil { + if col.Args == nil && q.table != nil { if field, ok := q.table.FieldMap[col.Query]; ok { b = append(b, q.table.SQLAlias...) b = append(b, '.')