Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code 39a7adf588 Missing table alias in ORDER BY for date fields causes ambiguous column error
https://sonarly.com/issue/6393?type=bug

The ORDER BY clause for date-granularity fields in bar chart queries is generated without a table alias prefix, causing PostgreSQL "column reference is ambiguous" errors when the chart configuration includes a JOIN to a related table that has a column with the same name.

Fix: The fix adds the table alias prefix to the `columnNameWithQuotes` construction in `parseObjectRecordOrderByWithGroupByDateField`. Previously, the method built the column reference as just `"createdAt"` (no table qualifier), while the SELECT and GROUP BY clauses correctly used `"opportunity"."createdAt"`. This caused PostgreSQL to raise an ambiguous column reference error when a JOIN brought in a related table with a column of the same name.

The fix splits the original one-liner into two statements — first computing the bare `columnName`, then building `columnNameWithQuotes` as `"${this.flatObjectMetadata.nameSingular}"."${columnName}"` — exactly matching the pattern used in `getGroupByDefinitions` and `parseObjectRecordOrderByForScalarField`.

```typescript file=packages/twenty-server/src/engine/api/graphql/graphql-query-runner/graphql-query-parsers/graphql-query-order/graphql-query-order-group-by.parser.ts lines=500-506
    const columnName = formatColumnNamesFromCompositeFieldAndSubfields(
      associatedGroupByField.fieldMetadata.name,
      associatedGroupByField.subFieldName
        ? [associatedGroupByField.subFieldName]
        : undefined,
    )[0];
    const columnNameWithQuotes = `"${this.flatObjectMetadata.nameSingular}"."${columnName}"`;
```

No call-site changes or method signature changes are needed because `this.flatObjectMetadata` is already a class property, accessible directly within the private method.
2026-03-05 03:36:34 +00:00
@@ -497,14 +497,13 @@ export class GraphqlQueryOrderGroupByParser {
);
}
const columnNameWithQuotes = `"${
formatColumnNamesFromCompositeFieldAndSubfields(
associatedGroupByField.fieldMetadata.name,
associatedGroupByField.subFieldName
? [associatedGroupByField.subFieldName]
: undefined,
)[0]
}"`;
const columnName = formatColumnNamesFromCompositeFieldAndSubfields(
associatedGroupByField.fieldMetadata.name,
associatedGroupByField.subFieldName
? [associatedGroupByField.subFieldName]
: undefined,
)[0];
const columnNameWithQuotes = `"${this.flatObjectMetadata.nameSingular}"."${columnName}"`;
const expression = getGroupByOrderExpression({
groupByField: associatedGroupByField,