Skip to content

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

Merged
hailaz merged 4 commits intogogf:masterfrom
lingcoder:bugfix/order-alias
Dec 26, 2025
Merged

refactor(database/gdb): simplify order and group by alias quoting (bu…#4555
hailaz merged 4 commits intogogf:masterfrom
lingcoder:bugfix/order-alias

Conversation

@lingcoder
Copy link
Contributor

@lingcoder lingcoder commented Dec 9, 2025

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

@gqcn gqcn added the wip label Dec 11, 2025
@gqcn
Copy link
Member

gqcn commented Dec 11, 2025

Thanks for your suggestion! We'll carefully consider and test it, though this may take a little time. We also welcome other guys who see this to join in code review and participate in the discussion.

@hailaz hailaz removed the wip label Dec 26, 2025
@hailaz
Copy link
Contributor

hailaz commented Dec 26, 2025

撤销 #4521 的更改,自动前缀提高维护的复杂度,减少了透明度,增加了调试的难度。手动设置前缀更加直观和可预测。

@hailaz hailaz merged commit 7daf916 into gogf:master Dec 26, 2025
18 checks passed
@lingcoder lingcoder deleted the bugfix/order-alias branch February 28, 2026 00:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants