diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index 75964af513..66d6f28a5b 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -34,6 +34,7 @@ import { getBindings } from "components/backend/DataTable/formula" import JSONSchemaModal from "./JSONSchemaModal.svelte" import { ValidColumnNameRegex } from "@budibase/shared-core" + import RelationshipSelector from "components/common/RelationshipSelector.svelte" const AUTO_TYPE = "auto" const FORMULA_TYPE = FIELDS.FORMULA.type @@ -57,6 +58,10 @@ let indexes = [...($tables.selected.indexes || [])] let isCreating = undefined + let relationshipPart1 = "Many rows" + let relationshipPart2 = "To one row" + + let relationshipTableIdSecondary = null let table = $tables.selected let confirmDeleteDialog let savingColumn @@ -74,6 +79,32 @@ editableColumn.constraints.presence = { allowEmpty: false } } + $: { + console.log("test") + if (editableColumn.type === LINK_TYPE) { + // Determine the relationship type based on the selected values of both parts + if ( + relationshipPart1 === "Many rows" && + relationshipPart2 === "To one row" + ) { + editableColumn.relationshipType = RelationshipType.MANY_TO_ONE + } else if ( + relationshipPart1 === "Many rows" && + relationshipPart2 === "To many rows" + ) { + editableColumn.relationshipType = RelationshipType.MANY_TO_MANY + } else if ( + relationshipPart1 === "One row" && + relationshipPart2 === "To many rows" + ) { + editableColumn.relationshipType = RelationshipType.ONE_TO_MANY + } + + // Set the tableId based on the selected table + editableColumn.tableId = relationshipTableIdSecondary + } + } + const initialiseField = (field, savingColumn) => { isCreating = !field if (field && !savingColumn) { @@ -100,6 +131,28 @@ } } allowedTypes = getAllowedTypes() + + if (editableColumn.type === LINK_TYPE && editableColumn.tableId) { + relationshipTableIdSecondary = editableColumn.tableId + console.log("test?") + console.log(editableColumn.relationshipType) + console.log(RelationshipType.MANY_TO_MANY) + if (editableColumn.relationshipType === RelationshipType.MANY_TO_ONE) { + relationshipPart1 = "Many rows" + relationshipPart2 = "To one row" + } else if ( + editableColumn.relationshipType === RelationshipType.MANY_TO_MANY + ) { + console.log("asdasdasd?") + relationshipPart1 = "Many rows" + relationshipPart2 = "To many rows" + } else if ( + editableColumn.relationshipType === RelationshipType.ONE_TO_MANY + ) { + relationshipPart1 = "One row" + relationshipPart2 = "To many rows" + } + } } $: initialiseField(field, savingColumn) @@ -546,30 +599,15 @@ {:else if editableColumn.type === "link"} - {:else if editableColumn.type === FORMULA_TYPE} {#if !table.sql} diff --git a/packages/builder/src/components/common/RelationshipSelector.svelte b/packages/builder/src/components/common/RelationshipSelector.svelte new file mode 100644 index 0000000000..d196eaeaad --- /dev/null +++ b/packages/builder/src/components/common/RelationshipSelector.svelte @@ -0,0 +1,69 @@ + + +
+
+ +
+
+
+
+ table.name} + getOptionValue={table => table._id} + /> +
+
+ + +