From 619f68fc3b545b780cefeb74cf3f6d01621af7e2 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 19 Feb 2021 10:17:11 +0000 Subject: [PATCH] Fixing an issue found by Andrew K - relationship lookup needs to work on both sides of relationship. --- packages/server/src/db/linkedRows/index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/server/src/db/linkedRows/index.js b/packages/server/src/db/linkedRows/index.js index 5d1da9abd1..65985dead2 100644 --- a/packages/server/src/db/linkedRows/index.js +++ b/packages/server/src/db/linkedRows/index.js @@ -36,6 +36,20 @@ function getLinkedTableIDs(table) { .map(column => column.tableId) } +function getRelatedTableForField(table, fieldName) { + // look to see if its on the table, straight in the schema + const field = table.schema[fieldName] + if (field != null) { + return field.tableId + } + for (let column of Object.values(table.schema)) { + if (column.type === FieldTypes.LINK && column.fieldName === fieldName) { + return column.tableId + } + } + return null +} + async function getLinksForRows(appId, rows) { const tableIds = [...new Set(rows.map(el => el.tableId))] // start by getting all the link values for performance reasons @@ -185,7 +199,7 @@ exports.attachLinkedPrimaryDisplay = async (appId, table, rows) => { if (row[link.fieldName] == null) { row[link.fieldName] = [] } - const linkedTableId = table.schema[link.fieldName].tableId + const linkedTableId = getRelatedTableForField(table, link.fieldName) const linkedRow = linked.find(row => row._id === link.id) const linkedTable = linkedTables.find( table => table._id === linkedTableId