From f77b596a075fd2b3c6c484b6d8f63c982fe54807 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 11 Feb 2022 14:04:23 +0000 Subject: [PATCH] Fix for #4431 - converting SQL joins back to left joins, syntax specifying join type as string is old which doesn't work in latest versions of knex. --- .../api/controllers/row/ExternalRequest.ts | 2 +- packages/server/src/integrations/base/sql.ts | 68 ++++++++----------- 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index 6aa51fb36b..c1181dd6d5 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -541,7 +541,7 @@ module External { if (!linkTable || !linkPrimary) { return } - const rows = related[key].rows || [] + const rows = related[key]?.rows || [] const found = rows.find( (row: { [key: string]: any }) => row[linkPrimary] === relationship.id || diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index a4220565cf..ce06624107 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -210,49 +210,37 @@ class InternalBuilder { const { toTable, throughTable } = JSON.parse(key) if (!throughTable) { // @ts-ignore - query = query.join( - toTable, - function () { - for (let relationship of relationships) { - const from = relationship.from, - to = relationship.to - // @ts-ignore - this.orOn(`${fromTable}.${from}`, "=", `${toTable}.${to}`) - } - }, - "left" - ) + query = query.leftJoin(toTable, function () { + for (let relationship of relationships) { + const from = relationship.from, + to = relationship.to + // @ts-ignore + this.orOn(`${fromTable}.${from}`, "=", `${toTable}.${to}`) + } + }) } else { query = query // @ts-ignore - .join( - throughTable, - function () { - for (let relationship of relationships) { - const fromPrimary = relationship.fromPrimary - const from = relationship.from - // @ts-ignore - this.orOn( - `${fromTable}.${fromPrimary}`, - "=", - `${throughTable}.${from}` - ) - } - }, - "left" - ) - .join( - toTable, - function () { - for (let relationship of relationships) { - const toPrimary = relationship.toPrimary - const to = relationship.to - // @ts-ignore - this.orOn(`${toTable}.${toPrimary}`, `${throughTable}.${to}`) - } - }, - "left" - ) + .leftJoin(throughTable, function () { + for (let relationship of relationships) { + const fromPrimary = relationship.fromPrimary + const from = relationship.from + // @ts-ignore + this.orOn( + `${fromTable}.${fromPrimary}`, + "=", + `${throughTable}.${from}` + ) + } + }) + .leftJoin(toTable, function () { + for (let relationship of relationships) { + const toPrimary = relationship.toPrimary + const to = relationship.to + // @ts-ignore + this.orOn(`${toTable}.${toPrimary}`, `${throughTable}.${to}`) + } + }) } } return query.limit(BASE_LIMIT)