Prevent duplicates in select list. Fix taken from PR 3498 and fixes #2930 (#4140)

Co-authored-by: Martijn Boland <martijn@taiga.nl>
This commit is contained in:
Martijn Boland
2021-02-18 00:59:43 +01:00
committed by GitHub
parent 6661633031
commit d95ab172bd

View File

@@ -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 };
}