From 3d767d52c0c20e289148daf78fe4f1c898448b21 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 13 Dec 2024 13:30:39 +0100 Subject: [PATCH] Relations, select only required fields --- .../src/api/controllers/row/utils/sqlUtils.ts | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/controllers/row/utils/sqlUtils.ts b/packages/server/src/api/controllers/row/utils/sqlUtils.ts index b67ef6be92..51fba4bb79 100644 --- a/packages/server/src/api/controllers/row/utils/sqlUtils.ts +++ b/packages/server/src/api/controllers/row/utils/sqlUtils.ts @@ -164,8 +164,24 @@ export async function buildSqlFieldList( } const { tableName } = breakExternalTableId(field.tableId) - if (tables[tableName]) { - fields = fields.concat(extractRealFields(tables[tableName], fields)) + const relatedTable = tables[tableName] + if (relatedTable) { + const viewFields = new Set() + relatedTable.primary?.forEach(f => viewFields.add(f)) + if (relatedTable.primaryDisplay) { + viewFields.add(relatedTable.primaryDisplay) + } + + if (isView) { + Object.entries(source.schema?.[field.name].columns || {}) + .filter(([_, column]) => helpers.views.isVisible(column)) + .forEach(([field]) => viewFields.add(field)) + } + + const fieldsToAdd = Array.from(viewFields) + .map(f => `${relatedTable.name}.${f}`) + .filter(f => !fields.includes(f)) + fields.push(...fieldsToAdd) } }