From 5a0ef45c2976f93cf99eb4ad9a6944a4e55cede5 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 5 Aug 2021 19:49:30 +0100 Subject: [PATCH] Fixing issues with many to many relationships in SQL, sometimes not creating right relationships. --- .../api/controllers/row/ExternalRequest.ts | 24 ++++++++++++------- packages/server/src/integrations/base/sql.ts | 2 ++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index f729ee7ff3..4107937cbd 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -364,7 +364,7 @@ module External { } } if (cache[fullKey] == null) { - cache[fullKey] = await makeExternalQuery(this.appId, { + const response = await makeExternalQuery(this.appId, { endpoint: getEndpoint(tableId, DataSourceOperation.READ), filters: { equal: { @@ -372,8 +372,12 @@ module External { }, }, }) + // this is the response from knex if no rows found + if (!response[0].read) { + cache[fullKey] = response + } } - return { rows: cache[fullKey], table } + return { rows: cache[fullKey] || [], table } } /** @@ -423,12 +427,16 @@ module External { const { tableName } = breakExternalTableId(tableId) const table = this.tables[tableName] for (let row of rows) { - promises.push( - makeExternalQuery(this.appId, { - endpoint: getEndpoint(tableId, DataSourceOperation.DELETE), - filters: buildFilters(generateIdForRow(row, table), {}, table), - }) - ) + const filters = buildFilters(generateIdForRow(row, table), {}, table) + // safety check, if there are no filters on deletion bad things happen + if (Object.keys(filters).length !== 0) { + promises.push( + makeExternalQuery(this.appId, { + endpoint: getEndpoint(tableId, DataSourceOperation.DELETE), + filters, + }) + ) + } } } await Promise.all(promises) diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index 0587e3df1d..ca6dcb15fd 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -151,6 +151,8 @@ function buildRead(knex: Knex, json: QueryJson, limit: number): KnexQuery { } // handle select if (resource.fields && resource.fields.length > 0) { + // select the resources as the format "table.columnName" - this is what is provided + // by the resource builder further up query = query.select(resource.fields.map(field => `${field} as ${field}`)) } else { query = query.select("*")