Skip to content

Conversation

@ljluestc
Copy link
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

@hailaz hailaz requested a review from Copilot November 24, 2025 02:55
Copilot finished reviewing on behalf of hailaz November 24, 2025 02:57
Copy link
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
Copilot finished reviewing on behalf of hailaz November 24, 2025 07:11
Copy link
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
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.

2 participants