Fixing an issue with creating relationships between existing tables, not using the correct type for the primary key in the foreign key relationship.

This commit is contained in:
mike12345567 2023-04-19 17:17:37 +01:00
parent 401d64b074
commit 1aca5d6407
3 changed files with 20 additions and 13 deletions

View File

@ -391,8 +391,9 @@ export class ExternalRequest {
} }
} }
relatedRow = processFormulas(linkedTable, relatedRow) relatedRow = processFormulas(linkedTable, relatedRow)
const relatedDisplay = display ? relatedRow[display] : undefined
row[relationship.column][key] = { row[relationship.column][key] = {
primaryDisplay: display ? relatedRow[display] : undefined, primaryDisplay: relatedDisplay || "Invalid display column",
_id: relatedRow._id, _id: relatedRow._id,
} }
} }

View File

@ -79,10 +79,17 @@ function generateSchema(
if (!relatedTable) { if (!relatedTable) {
throw "Referenced table doesn't exist" 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 schema
.foreign(column.foreignKey) .foreign(column.foreignKey)
.references(`${tableName}.${relatedTable.primary[0]}`) .references(`${tableName}.${relatedPrimary}`)
} }
break break
} }

View File

@ -1,11 +1,11 @@
import { import {
FieldTypes,
FormulaTypes,
AutoFieldDefaultNames, AutoFieldDefaultNames,
AutoFieldSubTypes, AutoFieldSubTypes,
FieldTypes,
FormulaTypes,
} from "../../constants" } from "../../constants"
import { processStringSync } from "@budibase/string-templates" 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 * 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 const isStatic = schema.formulaType === FormulaTypes.STATIC
if ( if (
schema.type !== FieldTypes.FORMULA || schema.type !== FieldTypes.FORMULA ||
schema.formula == null ||
(dynamic && isStatic) || (dynamic && isStatic) ||
(!dynamic && !isStatic) (!dynamic && !isStatic)
) { ) {
@ -57,13 +58,11 @@ export function processFormulas(
} }
// iterate through rows and process formula // iterate through rows and process formula
for (let i = 0; i < rowArray.length; i++) { for (let i = 0; i < rowArray.length; i++) {
if (schema.formula) { let row = rowArray[i]
let row = rowArray[i] let context = contextRows ? contextRows[i] : row
let context = contextRows ? contextRows[i] : row rowArray[i] = {
rowArray[i] = { ...row,
...row, [column]: processStringSync(schema.formula, context),
[column]: processStringSync(schema.formula, context),
}
} }
} }
} }