gf icon indicating copy to clipboard operation
gf copied to clipboard

refactor(database/gdb): simplify order and group by alias quoting (bu…

Open lingcoder opened this issue 2 weeks ago • 0 comments

What this PR does

Revert the auto table prefix behavior in Order() and Group() introduced by #4521.

Why

PR #4521 attempted to resolve column ambiguity in GROUP BY/ORDER BY with MySQL JOIN by automatically adding table prefixes to unqualified columns. However, this approach has issues:

  1. When using .As() to set table alias, it uses the original table name instead of the alias, causing errors in PostgreSQL and other databases
  2. The framework cannot reliably determine which table the user intends when multiple tables have the same column
  3. Adds hidden behavior that users may not expect

Example of the issue (#4554)

db.Model("demo_a").As("a").
    LeftJoin("demo_b", "b", "a.id=b.data_id").
    Order("sort").All()

Expected (v2.9.5):
ORDER BY "sort"

Actual (v2.9.6):
ORDER BY "demo_a".sort  -- Wrong! Should use alias "a" or no prefix

Solution

Revert to v2.9.5 behavior: Order("sort") generates ORDER BY "sort" without auto-prefixing. Users should explicitly specify table prefix when needed:
Order("a.sort").

Closes #4554

lingcoder avatar Dec 09 '25 12:12 lingcoder