From 1aca5d6407b2264e5c373fb689c710afe6f8bf07 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 19 Apr 2023 17:17:37 +0100 Subject: [PATCH] Fixing an issue with creating relationships between existing tables, not using the correct type for the primary key in the foreign key relationship. --- .../api/controllers/row/ExternalRequest.ts | 3 ++- .../server/src/integrations/base/sqlTable.ts | 11 +++++++++-- .../src/utilities/rowProcessor/utils.ts | 19 +++++++++---------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index b38d6bc227..b13ec22cfd 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -391,8 +391,9 @@ export class ExternalRequest { } } relatedRow = processFormulas(linkedTable, relatedRow) + const relatedDisplay = display ? relatedRow[display] : undefined row[relationship.column][key] = { - primaryDisplay: display ? relatedRow[display] : undefined, + primaryDisplay: relatedDisplay || "Invalid display column", _id: relatedRow._id, } } diff --git a/packages/server/src/integrations/base/sqlTable.ts b/packages/server/src/integrations/base/sqlTable.ts index 58b2b7a3d1..3bdfb76c02 100644 --- a/packages/server/src/integrations/base/sqlTable.ts +++ b/packages/server/src/integrations/base/sqlTable.ts @@ -79,10 +79,17 @@ function generateSchema( if (!relatedTable) { throw "Referenced table doesn't exist" } - schema.integer(column.foreignKey).unsigned() + const relatedPrimary = relatedTable.primary[0] + const externalType = relatedTable.schema[relatedPrimary].externalType + if (externalType) { + schema.specificType(column.foreignKey, externalType) + } else { + schema.integer(column.foreignKey).unsigned() + } + schema .foreign(column.foreignKey) - .references(`${tableName}.${relatedTable.primary[0]}`) + .references(`${tableName}.${relatedPrimary}`) } break } diff --git a/packages/server/src/utilities/rowProcessor/utils.ts b/packages/server/src/utilities/rowProcessor/utils.ts index c22bc59419..66ca969663 100644 --- a/packages/server/src/utilities/rowProcessor/utils.ts +++ b/packages/server/src/utilities/rowProcessor/utils.ts @@ -1,11 +1,11 @@ import { - FieldTypes, - FormulaTypes, AutoFieldDefaultNames, AutoFieldSubTypes, + FieldTypes, + FormulaTypes, } from "../../constants" import { processStringSync } from "@budibase/string-templates" -import { FieldSchema, Table, Row } from "@budibase/types" +import { FieldSchema, FieldType, Row, Table } from "@budibase/types" /** * If the subtype has been lost for any reason this works out what @@ -50,6 +50,7 @@ export function processFormulas( const isStatic = schema.formulaType === FormulaTypes.STATIC if ( schema.type !== FieldTypes.FORMULA || + schema.formula == null || (dynamic && isStatic) || (!dynamic && !isStatic) ) { @@ -57,13 +58,11 @@ export function processFormulas( } // iterate through rows and process formula for (let i = 0; i < rowArray.length; i++) { - if (schema.formula) { - let row = rowArray[i] - let context = contextRows ? contextRows[i] : row - rowArray[i] = { - ...row, - [column]: processStringSync(schema.formula, context), - } + let row = rowArray[i] + let context = contextRows ? contextRows[i] : row + rowArray[i] = { + ...row, + [column]: processStringSync(schema.formula, context), } } }