From d95ab172bd1a39e9ec70fd179254804f97322021 Mon Sep 17 00:00:00 2001 From: Martijn Boland Date: Thu, 18 Feb 2021 00:59:43 +0100 Subject: [PATCH] Prevent duplicates in select list. Fix taken from PR 3498 and fixes #2930 (#4140) Co-authored-by: Martijn Boland --- api/src/database/run-ast.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/api/src/database/run-ast.ts b/api/src/database/run-ast.ts index d09f3bbfb4..1031a26b4d 100644 --- a/api/src/database/run-ast.ts +++ b/api/src/database/run-ast.ts @@ -96,13 +96,13 @@ async function parseCurrentLevel( const primaryKeyField = schema.tables[collection].primary; const columnsInCollection = Object.keys(schema.tables[collection].columns); - const columnsToSelect: string[] = []; + const columnsToSelectInternal: string[] = []; const nestedCollectionNodes: NestedCollectionNode[] = []; for (const child of children) { if (child.type === 'field') { if (columnsInCollection.includes(child.name) || child.name === '*') { - columnsToSelect.push(child.name); + columnsToSelectInternal.push(child.name); } continue; @@ -111,22 +111,25 @@ async function parseCurrentLevel( if (!child.relation) continue; if (child.type === 'm2o') { - columnsToSelect.push(child.relation.many_field); + columnsToSelectInternal.push(child.relation.many_field); } if (child.type === 'm2a') { - columnsToSelect.push(child.relation.many_field); - columnsToSelect.push(child.relation.one_collection_field!); + columnsToSelectInternal.push(child.relation.many_field); + columnsToSelectInternal.push(child.relation.one_collection_field!); } nestedCollectionNodes.push(child); } /** Always fetch primary key in case there's a nested relation that needs it */ - if (columnsToSelect.includes(primaryKeyField) === false) { - columnsToSelect.push(primaryKeyField); + if (columnsToSelectInternal.includes(primaryKeyField) === false) { + columnsToSelectInternal.push(primaryKeyField); } + /** Make sure select list has unique values */ + const columnsToSelect = [...new Set(columnsToSelectInternal)]; + return { columnsToSelect, nestedCollectionNodes, primaryKeyField }; }