Skip to content

fix(database/gdb): Resolve column ambiguity in GROUP BY/ORDER BY with MySQL JOIN#4521

Merged
hailaz merged 7 commits intogogf:masterfrom
ljluestc:ambiguous_group_mysql_join_queries
Nov 24, 2025
Merged

fix(database/gdb): Resolve column ambiguity in GROUP BY/ORDER BY with MySQL JOIN#4521
hailaz merged 7 commits intogogf:masterfrom
ljluestc:ambiguous_group_mysql_join_queries

Conversation

@ljluestc
Copy link
Copy Markdown
Contributor

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 (commonly id). This results in MySQL errors like "Column 'id' in group statement is ambiguous".

Example Issue:

model := t.Ctx(ctx).Fields("t_inf_job.*, t_inf_job_attr.*").
    LeftJoin("t_inf_job_attr", "t_inf_job.id = t_inf_job_attr.job_id").
    Where(t.Columns().Deleted, 0)

// This would fail with "Column 'id' in group statement is ambiguous"
err = model.Group(t.Columns().Id).Scan(&jobs)

Key Changes

  1. Modified function signature: Group(groupBy ...string)Group(groupBy ...any) to support Raw SQL expressions
  2. Auto-prefixing logic: When JOINs are detected (by checking for " JOIN " in the tables string), unqualified column names are automatically prefixed with the primary table name
  3. Preserved existing behavior: Already qualified columns (containing ".") and Raw expressions are handled as before
  4. Added comprehensive test: Test_Model_Group_WithJoin verifies the fix works correctly with JOIN queries

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ...any instead of ...string to 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.

@hailaz hailaz marked this pull request as ready for review November 24, 2025 04:01
@hailaz hailaz changed the title Ambiguous group mysql join queries fix(database/gdb): Resolve column ambiguity in GROUP BY/ORDER BY with MySQL JOIN Nov 24, 2025
@hailaz hailaz requested a review from Copilot November 24, 2025 07:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 hailaz merged commit fe8ba5e into gogf:master Nov 24, 2025
18 checks passed
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>
@hailaz
Copy link
Copy Markdown
Contributor

hailaz commented Dec 26, 2025

撤销了本次修改,详情 #4555

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