gf
gf copied to clipboard
bug(database/gdb): v2.9.6 Order() incorrectly adds original table name prefix instead of alias when using As()
Go version
go 1.25+
GoFrame version
v2.9.6
Can this bug be reproduced with the latest release?
Option Yes
What did you do?
When using .As("a") to set a table alias with LeftJoin, and then using Order(dao.Table.Columns().Field):
stickerCols := dao.DemoA.Columns()
err = dao.DemoA.Ctx(ctx).As("a").
LeftJoin(dao.DemoB.Table(), "b", "a.id=b.data_id").
Fields("a.id", "a.name").
Order(dao.DemoA.Columns().Sort).
Scan(&res)
What did you expect to see?
v2.9.5 generates (correct):
SELECT a.id, a.name
FROM "demo_a" AS a
LEFT JOIN "demo_b" AS "b" ON (a.id=b.data_id)
ORDER BY "sort"
LIMIT 10 OFFSET 0
The Order() should generate ORDER BY "sort" (column name only), as it did in v2.9.5.
### What did you see happen?
v2.9.6 generates (incorrect):
SELECT a.id, a.name
FROM "demo_a" AS a
LEFT JOIN "demo_b" AS "b" ON (a.id=b.data_id)
ORDER BY "demo_a".sort
LIMIT 10 OFFSET 0
v2.9.6 incorrectly adds the original table name prefix to the column in ORDER BY, instead of:
1. Keeping just the column name (as in v2.9.5), OR
2. Using the alias a.sort if a prefix is needed
Error Message (PostgreSQL)
pq: invalid reference to FROM-clause entry for table "demo_a"
HINT: Perhaps you meant to reference the table alias "a".
Root Cause Analysis
This issue was likely introduced by PR #4521 "Resolve column ambiguity in GROUP BY/ORDER BY with MySQL JOIN".
The fix correctly tries to add table prefixes to avoid ambiguity, but it uses the original table name instead of the alias set by .As().
### What did you expect to see?
v2.9.5 generates (correct):
SELECT a.id, a.name
FROM "demo_a" AS a
LEFT JOIN "demo_b" AS "b" ON (a.id=b.data_id)
ORDER BY "sort"
LIMIT 10 OFFSET 0
The Order() should generate ORDER BY "sort" (column name only), as it did in v2.9.5.
- If .As("a") is called, any auto-prefixing should use a.column, not original_table.column
- Or simply keep the column name as-is when it's unambiguous (v2.9.5 behavior)
Suggested Fix
When auto-prefixing columns in Order()/Group(), the framework should:
1. Check if an alias was set via .As()
2. If yes, use the alias as prefix (e.g., a.sort)
3. If no alias, use the original table name
Workaround
Manually specify the alias prefix:
// Instead of:
Order(dao.DemoA.Columns().Sort)
// Use:
Order("a.sort")