diff --git a/api/src/database/run-ast.ts b/api/src/database/run-ast.ts index bf4d67881d..43c45459dc 100644 --- a/api/src/database/run-ast.ts +++ b/api/src/database/run-ast.ts @@ -132,8 +132,11 @@ async function parseCurrentLevel( nestedCollectionNodes.push(child); } - /** Always fetch primary key in case there's a nested relation that needs it */ - if (columnsToSelectInternal.includes(primaryKeyField) === false) { + /** + * Always fetch primary key in case there's a nested relation that needs it + */ + const childrenContainRelational = children.some((child) => child.type !== 'field'); + if (childrenContainRelational && columnsToSelectInternal.includes(primaryKeyField) === false) { columnsToSelectInternal.push(primaryKeyField); } @@ -385,6 +388,17 @@ function removeTemporaryFields( } } + // Make sure any new aliased aggregate fields are included + if (ast.query?.aggregate) { + for (const [_operation, aliasMap] of Object.entries(ast.query.aggregate)) { + if (!aliasMap) continue; + + for (const [_column, alias] of Object.entries(aliasMap)) { + fields.push(alias); + } + } + } + for (const rawItem of rawItems) { if (rawItem === null || rawItem === undefined) return rawItem;