From 949c6b8653b61a0a18038e39da61b8730597dccf Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 28 Oct 2021 22:44:31 +0100 Subject: [PATCH] Adding SQL relationship building. --- .../DataTable/modals/CreateEditColumn.svelte | 6 ++- .../src/api/controllers/table/external.js | 41 ++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index 221d391cbf..54bbb7fadf 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -89,6 +89,10 @@ if (field.type === AUTO_TYPE) { field = buildAutoColumn($tables.draft.name, field.name, field.subtype) } + // for now you can't create other options, just many to many for SQL + if (field.type === LINK_TYPE && external) { + field.relationshipType = RelationshipTypes.ONE_TO_MANY + } await tables.saveField({ originalName, field, @@ -324,7 +328,7 @@ getOptionLabel={table => table.name} getOptionValue={table => table._id} /> - {#if relationshipOptions && relationshipOptions.length > 0} + {#if relationshipOptions && relationshipOptions.length > 0 && !external} table._id === schema.tableId ) + // setup the schema in this table const relatedField = schema.fieldName + const relatedPrimary = relatedTable.primary[0] + // generate a foreign key const foreignKey = `fk_${relatedTable.name}_${schema.fieldName}` - // create foreign key - tableToSave.schema[foreignKey] = { type: FieldTypes.NUMBER } - // setup the relation in other table and this one + + schema.relationshipType = RelationshipTypes.ONE_TO_MANY schema.foreignKey = foreignKey - schema.fieldName = foreignKey + schema.fieldName = relatedPrimary schema.main = true - const relatedSchema = cloneDeep(schema) - relatedSchema.fieldName = key - delete relatedSchema.main - relatedTable.schema[relatedField] = relatedSchema + + relatedTable.schema[relatedField] = generateRelatedSchema(schema, table) + tableToSave.schema[foreignKey] = { + type: FieldTypes.NUMBER, + constraints: {}, + } } }