From 60f86184de74ac37650f7ca5825a2df9a54be869 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 23 Nov 2021 12:10:40 +0000 Subject: [PATCH] Quick fix for #3485 - default the key to the primary key but allow another field to be selected. --- .../Datasources/CreateEditRelationship.svelte | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index 81032da716..8b984b5dea 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -71,6 +71,9 @@ if ($touched.toCol && !toRelate.name) { errors.toCol = colNotSet } + if ($touched.primary && !fromPrimary) { + errors.primary = "Please pick the primary key" + } // currently don't support relationships back onto the table itself, needs to relate out const tableError = "From/to/through tables must be different" if (fromTable && (fromTable === toTable || fromTable === throughTable)) { @@ -95,6 +98,12 @@ return errors } + $: isManyToMany = + fromRelationship?.relationshipType === RelationshipTypes.MANY_TO_MANY + $: isManyToOne = + fromRelationship?.relationshipType === RelationshipTypes.MANY_TO_ONE + $: fromPrimary = + !fromPrimary && fromTable ? fromTable.primary[0] : fromPrimary $: tableOptions = plusTables.map(table => ({ label: table.name, value: table._id, @@ -179,13 +188,13 @@ // foreignKey is what is linking out of the current table. relateFrom = { ...relateFrom, - foreignKey: fromTable.primary[0], + foreignKey: fromPrimary, } relateTo = { ...relateTo, relationshipType: RelationshipTypes.ONE_TO_MANY, foreignKey: relateFrom.fieldName, - fieldName: fromTable.primary[0], + fieldName: fromPrimary, } } @@ -264,6 +273,15 @@ bind:error={errors.from} bind:value={toRelationship.tableId} /> + {#if isManyToOne && fromTable} + - {#if fromRelationship?.relationshipType === RelationshipTypes.MANY_TO_MANY} + {#if isManyToMany}