From 9cc931acc4e12d8ef85d543080cb281a8fb7276e Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Wed, 21 Sep 2022 09:10:31 +0100 Subject: [PATCH 1/2] Do not allow non-match type primary foreign keys --- .../Datasources/CreateEditRelationship.svelte | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index 990aa736f6..eb78975b8b 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -90,6 +90,19 @@ if (inSchema(toTable, toRelate.name, originalToName)) { errObj.toCol = colError } + if ( + fromPrimary && + fromRelate.fieldName && + plusTables.filter(table => table.name === fromTable?.name)[0]?.schema[ + fromPrimary + ]?.type !== + plusTables.filter(table => table.name === toTable?.name)[0]?.schema[ + fromRelate.fieldName + ]?.type + ) { + errObj.foreign = + "Column type of the foreign key must match the primary key" + } errors = errObj } From 9a8b054b19109f90f30bd6aa0e2164e04103f25d Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Fri, 23 Sep 2022 16:54:39 +0100 Subject: [PATCH 2/2] Refactor --- .../Datasources/CreateEditRelationship.svelte | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index eb78975b8b..230748b577 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -90,16 +90,13 @@ if (inSchema(toTable, toRelate.name, originalToName)) { errObj.toCol = colError } - if ( - fromPrimary && - fromRelate.fieldName && - plusTables.filter(table => table.name === fromTable?.name)[0]?.schema[ - fromPrimary - ]?.type !== - plusTables.filter(table => table.name === toTable?.name)[0]?.schema[ - fromRelate.fieldName - ]?.type - ) { + + let fromType, toType + if (fromPrimary && fromRelate.fieldName) { + fromType = fromTable?.schema[fromPrimary]?.type + toType = toTable?.schema[fromRelate.fieldName]?.type + } + if (fromType && toType && fromType !== toType) { errObj.foreign = "Column type of the foreign key must match the primary key" }