From 94d3466113df78620dab26905ff20f4789af5622 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 5 Nov 2024 11:24:23 +0100 Subject: [PATCH 1/3] Allow setting multiple many-to-many relationships --- .../Datasources/CreateEditRelationship.svelte | 25 ++++++++----------- .../backend/Datasources/relationshipErrors.js | 6 ++--- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index b54ecbf9fd..c8aaee0695 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -65,7 +65,7 @@ let tableOptions let errorChecker = new RelationshipErrorChecker( invalidThroughTable, - relationshipExists + manyToManyRelationshipExistsFn ) let errors = {} let fromPrimary, fromForeign, fromColumn, toColumn @@ -125,7 +125,7 @@ } return false } - function relationshipExists() { + function manyToManyRelationshipExistsFn() { if ( originalFromTable && originalToTable && @@ -141,16 +141,14 @@ datasource.entities[getTable(toId).name].schema ).filter(value => value.through) - const matchAgainstUserInput = (fromTableId, toTableId) => - (fromTableId === fromId && toTableId === toId) || - (fromTableId === toId && toTableId === fromId) + const matchAgainstUserInput = link => + (link.throughTo === throughToKey && + link.throughFrom === throughFromKey) || + (link.throughTo === throughFromKey && link.throughFrom === throughToKey) - return !!fromThroughLinks.find(from => - toThroughLinks.find( - to => - from.through === to.through && - matchAgainstUserInput(from.tableId, to.tableId) - ) + const allLinks = [...fromThroughLinks, ...toThroughLinks] + return !!allLinks.find( + link => link.through === throughId && matchAgainstUserInput(link) ) } @@ -181,16 +179,15 @@ relationshipType: errorChecker.relationshipTypeSet(relationshipType), fromTable: errorChecker.tableSet(fromTable) || - errorChecker.doesRelationshipExists() || errorChecker.differentTables(fromId, toId, throughId), toTable: errorChecker.tableSet(toTable) || - errorChecker.doesRelationshipExists() || errorChecker.differentTables(toId, fromId, throughId), throughTable: errorChecker.throughTableSet(throughTable) || errorChecker.throughIsNullable() || - errorChecker.differentTables(throughId, fromId, toId), + errorChecker.differentTables(throughId, fromId, toId) || + errorChecker.doesRelationshipExists(), throughFromKey: errorChecker.manyForeignKeySet(throughFromKey) || errorChecker.manyTypeMismatch( diff --git a/packages/builder/src/components/backend/Datasources/relationshipErrors.js b/packages/builder/src/components/backend/Datasources/relationshipErrors.js index 610ff9f1fe..2088a55b81 100644 --- a/packages/builder/src/components/backend/Datasources/relationshipErrors.js +++ b/packages/builder/src/components/backend/Datasources/relationshipErrors.js @@ -30,9 +30,9 @@ function typeMismatchCheck(fromTable, toTable, primary, foreign) { } export class RelationshipErrorChecker { - constructor(invalidThroughTableFn, relationshipExistsFn) { + constructor(invalidThroughTableFn, manyToManyRelationshipExistsFn) { this.invalidThroughTable = invalidThroughTableFn - this.relationshipExists = relationshipExistsFn + this.manyToManyRelationshipExists = manyToManyRelationshipExistsFn } setType(type) { @@ -72,7 +72,7 @@ export class RelationshipErrorChecker { } doesRelationshipExists() { - return this.isMany() && this.relationshipExists() + return this.isMany() && this.manyToManyRelationshipExists() ? relationshipAlreadyExists : null } From 6505f518c8487249f576b3fe73f6524e252aac77 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 5 Nov 2024 11:31:46 +0100 Subject: [PATCH 2/3] Don't allow same foreign keys --- .../backend/Datasources/CreateEditRelationship.svelte | 3 ++- .../components/backend/Datasources/relationshipErrors.js | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index c8aaee0695..8a0bcce91e 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -195,7 +195,8 @@ throughTable, fromTable.primary[0], throughToKey - ), + ) || + errorChecker.differentColumns(throughFromKey, throughToKey), throughToKey: errorChecker.manyForeignKeySet(throughToKey) || errorChecker.manyTypeMismatch( diff --git a/packages/builder/src/components/backend/Datasources/relationshipErrors.js b/packages/builder/src/components/backend/Datasources/relationshipErrors.js index 2088a55b81..9fd30eaea2 100644 --- a/packages/builder/src/components/backend/Datasources/relationshipErrors.js +++ b/packages/builder/src/components/backend/Datasources/relationshipErrors.js @@ -3,6 +3,7 @@ import { RelationshipType } from "@budibase/types" const typeMismatch = "Column type of the foreign key must match the primary key" const columnBeingUsed = "Column name cannot be an existing column" const mustBeDifferentTables = "From/to/through tables must be different" +const mustBeDifferentColumns = "Foreign keys must be different" const primaryKeyNotSet = "Please pick the primary key" const throughNotNullable = "Ensure non-key columns are nullable or auto-generated" @@ -83,6 +84,11 @@ export class RelationshipErrorChecker { return error ? mustBeDifferentTables : null } + differentColumns(columnA, columnB) { + const error = columnA && columnB && columnA === columnB + return error ? mustBeDifferentColumns : null + } + columnBeingUsed(table, column, ogName) { return isColumnNameBeingUsed(table, column, ogName) ? columnBeingUsed : null } From 3cb653eb0146e32a676d488654b8436ae03ad391 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 5 Nov 2024 11:54:09 +0100 Subject: [PATCH 3/3] Fix initial setup --- .../backend/Datasources/CreateEditRelationship.svelte | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index 8a0bcce91e..7e11c98768 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -370,6 +370,16 @@ fromColumn = selectedFromTable.name fromPrimary = selectedFromTable?.primary[0] || null } + if (relationshipType === RelationshipType.MANY_TO_MANY) { + relationshipPart1 = PrettyRelationshipDefinitions.MANY + relationshipPart2 = PrettyRelationshipDefinitions.MANY + } else if (relationshipType === RelationshipType.MANY_TO_ONE) { + relationshipPart1 = PrettyRelationshipDefinitions.ONE + relationshipPart2 = PrettyRelationshipDefinitions.MANY + } else { + relationshipPart1 = PrettyRelationshipDefinitions.MANY + relationshipPart2 = PrettyRelationshipDefinitions.ONE + } })