diff --git a/src/select-query-parser/result.ts b/src/select-query-parser/result.ts index 3510a309..07802c70 100644 --- a/src/select-query-parser/result.ts +++ b/src/select-query-parser/result.ts @@ -165,21 +165,21 @@ type ProcessSimpleField< Row extends Record, RelationName extends string, Field extends Ast.FieldNode -> = Field['aggregateFunction'] extends AggregateFunctions - ? { - // An aggregate function will always override the column name id.sum() will become sum - // except if it has been aliased - [K in GetFieldNodeResultName]: Field['castType'] extends PostgreSQLTypes - ? TypeScriptTypes - : number - } - : [Field['name']] extends [keyof Row] - ? { - // Aliases override the property name in the result - [K in GetFieldNodeResultName]: Field['castType'] extends PostgreSQLTypes // We apply the detected casted as the result type - ? TypeScriptTypes - : Row[Field['name']] - } +> = Field['name'] extends keyof Row | 'count' + ? Field['aggregateFunction'] extends AggregateFunctions + ? { + // An aggregate function will always override the column name id.sum() will become sum + // except if it has been aliased + [K in GetFieldNodeResultName]: Field['castType'] extends PostgreSQLTypes + ? TypeScriptTypes + : number + } + : { + // Aliases override the property name in the result + [K in GetFieldNodeResultName]: Field['castType'] extends PostgreSQLTypes // We apply the detected casted as the result type + ? TypeScriptTypes + : Row[Field['name']] + } : SelectQueryError<`column '${Field['name']}' does not exist on '${RelationName}'.`> /** diff --git a/test/relationships.ts b/test/relationships.ts index 2a487d49..2660b585 100644 --- a/test/relationships.ts +++ b/test/relationships.ts @@ -177,6 +177,10 @@ export const selectParams = { from: 'collections', select: '*, parent_id(*)', }, + aggregateOnMissingColumnWithAlias: { + from: 'users', + select: 'alias:missing_column.count()', + }, } as const export const selectQueries = { @@ -349,6 +353,9 @@ export const selectQueries = { selfReferenceRelationViaColumn: postgrest .from(selectParams.selfReferenceRelationViaColumn.from) .select(selectParams.selfReferenceRelationViaColumn.select), + aggregateOnMissingColumnWithAlias: postgrest + .from(selectParams.aggregateOnMissingColumnWithAlias.from) + .select(selectParams.aggregateOnMissingColumnWithAlias.select), } as const test('nested query with selective fields', async () => { @@ -1806,3 +1813,21 @@ test('self reference relation via column', async () => { } `) }) + +test('aggregate on missing column with alias', async () => { + const res = await selectQueries.aggregateOnMissingColumnWithAlias.eq('id', 1).limit(1).single() + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": null, + "error": Object { + "code": "42703", + "details": null, + "hint": null, + "message": "column users.missing_column does not exist", + }, + "status": 400, + "statusText": "Bad Request", + } + `) +}) diff --git a/test/select-query-parser/select.test-d.ts b/test/select-query-parser/select.test-d.ts index e23de8a6..7849ea1f 100644 --- a/test/select-query-parser/select.test-d.ts +++ b/test/select-query-parser/select.test-d.ts @@ -736,3 +736,10 @@ type Schema = Database['public'] } expectType>(true) } + +// aggregate on missing column with alias +{ + const { data, error } = await selectQueries.aggregateOnMissingColumnWithAlias.limit(1).single() + if (error) throw error + expectType>(data) +}