Skip to content

Conversation

realtemirov
Copy link

Fix SQL errors in trend query builder

  1. Ambiguous column error (created_at) for join cases
  • Issue:
    When I use joins, Postgres raised:
SQLSTATE[42702]: Ambiguous column: 7 
ERROR: column reference "created_at" is ambiguous
  • Cause:
    After joining tables, the query should reference columns as "table"."column".
    But in our case it was generated as "table.column", which Postgres treated as a single literal column name instead of a qualified column.
  • Fix:
    Removed extra quotes when building the expression.
// Before
return "to_char(\"{$column}\", '{$format}')";

// After
return "to_char({$column}, '{$format}')";
  1. Grouping error (table.id)
  • Issue:
    Postgres raised:
SQLSTATE[42803]: Grouping error: 7 
ERROR: column "table.id" must appear in the GROUP BY clause or be used in an aggregate function
  • Cause:
    Query only grouped by the date alias, but selected table.*.
  • Fix:
    Explicitly added group by fields to avoid mismatch.
// Before
->groupBy($this->dateAlias)

// After
->groupBy($this->groupBy)

✅ With these fixes:

  1. Ambiguous created_at references are resolved.
  2. Grouping is aligned with selected columns.

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.

1 participant