fix(database/gdb): Resolve column ambiguity in GROUP BY/ORDER BY with MySQL JOIN#4521
Merged
hailaz merged 7 commits intogogf:masterfrom Nov 24, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses the issue of ambiguous column names in MySQL GROUP BY clauses when using JOIN queries. When multiple tables share common column names (like id), unqualified column references in GROUP BY cause MySQL errors.
Key Changes:
- Changed
Group()method signature to accept...anyinstead of...stringto support Raw SQL expressions - Added auto-prefixing logic that detects JOINs and qualifies unqualified column names with the primary table name
- Preserved handling for already-qualified columns and Raw expressions
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
database/gdb/gdb_model_order_group.go |
Modified Group() method to auto-prefix unqualified columns when JOINs are detected, supporting both string and Raw expression inputs |
contrib/drivers/mysql/mysql_z_unit_feature_model_join_test.go |
Added test case verifying GROUP BY works with JOIN queries without ambiguous column errors |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
hailaz
approved these changes
Nov 24, 2025
hailaz
added a commit
that referenced
this pull request
Dec 26, 2025
#4555) ## 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) ```go 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 ``` --------- Co-authored-by: hailaz <739476267@qq.com>
Contributor
|
撤销了本次修改,详情 #4555 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using JOIN queries in MySQL with the
Group()method, column names in GROUP BY clauses become ambiguous if multiple tables contain columns with the same name (commonlyid). This results in MySQL errors like "Column 'id' in group statement is ambiguous".Example Issue:
Key Changes
Group(groupBy ...string)→Group(groupBy ...any)to support Raw SQL expressionsTest_Model_Group_WithJoinverifies the fix works correctly with JOIN queries