diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index 81032da716..104fd7cb5a 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,16 @@ return errors } + let fromPrimary + $: { + if (!fromPrimary && fromTable) { + fromPrimary = fromTable.primary[0] + } + } + $: isManyToMany = + fromRelationship?.relationshipType === RelationshipTypes.MANY_TO_MANY + $: isManyToOne = + fromRelationship?.relationshipType === RelationshipTypes.MANY_TO_ONE $: tableOptions = plusTables.map(table => ({ label: table.name, value: table._id, @@ -179,13 +192,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 +277,15 @@ bind:error={errors.from} bind:value={toRelationship.tableId} /> + {#if isManyToOne && fromTable} + - {#if fromRelationship?.relationshipType === RelationshipTypes.MANY_TO_MANY} + {#if isManyToMany}