From 30905d35efca86013727435d0dbbc65596ca5d5c Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Fri, 13 Jan 2023 18:14:34 +0000 Subject: [PATCH] Remove existing relationships if table changed --- .../Datasources/CreateEditRelationship.svelte | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index 6263a3925a..1be842280f 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -245,12 +245,22 @@ datasource.entities[toTable.name].schema[toRelationship.name] = toRelationship - // If relationship has been renamed - if (originalFromName !== fromRelationship.name) { - delete datasource.entities[fromTable.name].schema[originalFromName] + // If relationship has been renamed or a different table selected + if ( + originalFromTable?.name && + (originalFromName !== fromRelationship.name || + hasTableChanged(fromTable, toTable)) + ) { + delete datasource.entities[originalFromTable.name].schema[ + originalFromName + ] } - if (originalToName !== toRelationship.name) { - delete datasource.entities[toTable.name].schema[originalToName] + if ( + originalToTable?.name && + (originalToName !== toRelationship.name || + hasTableChanged(fromTable, toTable)) + ) { + delete datasource.entities[originalToTable.name].schema[originalToName] } // store the original names so it won't cause an error