Merge pull request #10352 from Budibase/fix/10349

Allow SQL formulas using related row information to be used as display columns
This commit is contained in:
Michael Drury 2023-04-24 17:50:49 +01:00 committed by GitHub
commit 9c93bf91c0
4 changed files with 41 additions and 24 deletions

View File

@ -1,31 +1,32 @@
import {
Datasource,
FieldSchema,
FieldType,
FilterType,
IncludeRelationship,
Operation,
PaginationJson,
RelationshipsJson,
RelationshipTypes,
Row,
SearchFilters,
SortJson,
Datasource,
FieldSchema,
Row,
Table,
RelationshipTypes,
FieldType,
SortType,
Table,
} from "@budibase/types"
import {
breakExternalTableId,
breakRowIdField,
convertRowId,
generateRowIdField,
isRowId,
convertRowId,
isSQL,
} from "../../../integrations/utils"
import { getDatasourceAndQuery } from "./utils"
import { FieldTypes } from "../../../constants"
import { breakExternalTableId, isSQL } from "../../../integrations/utils"
import { processObjectSync } from "@budibase/string-templates"
import { cloneDeep } from "lodash/fp"
import { processFormulas, processDates } from "../../../utilities/rowProcessor"
import { processDates, processFormulas } from "../../../utilities/rowProcessor"
import { db as dbCore } from "@budibase/backend-core"
import sdk from "../../../sdk"
@ -382,10 +383,18 @@ export class ExternalRequest {
}
const display = linkedTable.primaryDisplay
for (let key of Object.keys(row[relationship.column])) {
const related: Row = row[relationship.column][key]
let relatedRow: Row = row[relationship.column][key]
// add this row as context for the relationship
for (let col of Object.values(linkedTable.schema)) {
if (col.type === FieldType.LINK && col.tableId === table._id) {
relatedRow[col.name] = [row]
}
}
relatedRow = processFormulas(linkedTable, relatedRow)
const relatedDisplay = display ? relatedRow[display] : undefined
row[relationship.column][key] = {
primaryDisplay: display ? related[display] : undefined,
_id: related._id,
primaryDisplay: relatedDisplay || "Invalid display column",
_id: relatedRow._id,
}
}
}

View File

@ -921,6 +921,7 @@ describe("row api - postgres", () => {
[m2mFieldName]: [
{
_id: row._id,
primaryDisplay: "Invalid display column",
},
],
})
@ -929,6 +930,7 @@ describe("row api - postgres", () => {
[m2mFieldName]: [
{
_id: row._id,
primaryDisplay: "Invalid display column",
},
],
})

View File

@ -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
}

View File

@ -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),
}
}
}