From 70ab14319d8061fee391d0472e5f9a325d902698 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 9 Oct 2024 16:51:11 +0100 Subject: [PATCH] Adding test case for removing from many side of relationships in SQL. --- .../api/controllers/row/ExternalRequest.ts | 22 ++++++++++++---- .../server/src/api/routes/tests/row.spec.ts | 26 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index 62ceedb56b..3510b5ed75 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -4,6 +4,7 @@ import { AutoFieldSubType, AutoReason, Datasource, + DatasourcePlusQueryResponse, FieldSchema, FieldType, FilterType, @@ -557,8 +558,9 @@ export class ExternalRequest { return matchesPrimaryLink } - const matchesSecondayLink = row[linkSecondary] === body?.[linkSecondary] - return matchesPrimaryLink && matchesSecondayLink + const matchesSecondaryLink = + row[linkSecondary] === body?.[linkSecondary] + return matchesPrimaryLink && matchesSecondaryLink } const existingRelationship = rows.find((row: { [key: string]: any }) => @@ -743,9 +745,19 @@ export class ExternalRequest { // aliasing can be disabled fully if desired const aliasing = new sdk.rows.AliasTables(Object.keys(this.tables)) - let response = env.SQL_ALIASING_DISABLE - ? await getDatasourceAndQuery(json) - : await aliasing.queryWithAliasing(json, makeExternalQuery) + let response: DatasourcePlusQueryResponse + // there's a chance after input processing nothing needs updated, so pass over the call + // we might still need to perform other operations like updating the foreign keys on other rows + if ( + this.operation === Operation.UPDATE && + Object.keys(row || {}).length === 0 + ) { + response = [config.row!] + } else { + response = env.SQL_ALIASING_DISABLE + ? await getDatasourceAndQuery(json) + : await aliasing.queryWithAliasing(json, makeExternalQuery) + } // if it's a counting operation there will be no more processing, just return the number if (this.operation === Operation.COUNT) { diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 0995bd3824..6f56916ddc 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -1114,6 +1114,32 @@ describe.each([ expect(getResp.user2[0]._id).toEqual(user2._id) }) + it("should be able to remove a relationship from many side", async () => { + const row = await config.api.row.save(otherTable._id!, { + name: "test", + description: "test", + }) + const row2 = await config.api.row.save(otherTable._id!, { + name: "test", + description: "test", + }) + const { _id } = await config.api.row.save(table._id!, { + relationship: [{ _id: row._id }, { _id: row2._id }], + }) + const relatedRow = await config.api.row.get(table._id!, _id!, { + status: 200, + }) + expect(relatedRow.relationship.length).toEqual(2) + await config.api.row.save(table._id!, { + ...relatedRow, + relationship: [{ _id: row._id }], + }) + const afterRelatedRow = await config.api.row.get(table._id!, _id!, { + status: 200, + }) + expect(afterRelatedRow.relationship.length).toEqual(1) + }) + it("should be able to update relationships when both columns are same name", async () => { let row = await config.api.row.save(table._id!, { name: "test",