diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index f6b75aca05..0139147e35 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -30,6 +30,7 @@ import { cloneDeep } from "lodash/fp" import { processDates, processFormulas } from "../../../utilities/rowProcessor" import { db as dbCore } from "@budibase/backend-core" import sdk from "../../../sdk" +import { isEditableColumn } from "../../../sdk/app/tables/validation" export interface ManyRelationship { tableId?: string @@ -298,8 +299,7 @@ export class ExternalRequest { if ( row[key] == null || newRow[key] || - field.autocolumn || - field.type === FieldTypes.FORMULA + !sdk.tables.isEditableColumn(field) ) { continue } diff --git a/packages/server/src/sdk/app/tables/index.ts b/packages/server/src/sdk/app/tables/index.ts index 92ef3f3291..fcb32f387b 100644 --- a/packages/server/src/sdk/app/tables/index.ts +++ b/packages/server/src/sdk/app/tables/index.ts @@ -7,7 +7,7 @@ import { } from "../../../integrations/utils" import { Table, Database } from "@budibase/types" import datasources from "../datasources" -import { checkExternalTableSchemas } from "./validation" +import { checkExternalTableSchemas, isEditableColumn } from "./validation" async function getAllInternalTables(db?: Database): Promise { if (!db) { @@ -62,4 +62,5 @@ export default { getExternalTable, getTable, checkExternalTableSchemas, + isEditableColumn, } diff --git a/packages/server/src/sdk/app/tables/validation.ts b/packages/server/src/sdk/app/tables/validation.ts index 98ad72ea3d..52385afb25 100644 --- a/packages/server/src/sdk/app/tables/validation.ts +++ b/packages/server/src/sdk/app/tables/validation.ts @@ -1,9 +1,11 @@ import { + AutoReason, Datasource, + FieldSchema, FieldType, RelationshipTypes, - AutoReason, } from "@budibase/types" +import { FieldTypes } from "../../../constants" function checkForeignKeysAreAutoColumns(datasource: Datasource) { if (!datasource.entities) { @@ -39,7 +41,8 @@ function checkForeignKeysAreAutoColumns(datasource: Datasource) { const shouldBeForeign = foreignKeys.find( options => options.tableId === table._id && options.key === column.name ) - if (shouldBeForeign) { + // don't change already auto-columns to it, e.g. primary keys that are foreign + if (shouldBeForeign && !column.autocolumn) { column.autocolumn = true column.autoReason = AutoReason.FOREIGN_KEY } else if (column.autoReason === AutoReason.FOREIGN_KEY) { @@ -52,6 +55,13 @@ function checkForeignKeysAreAutoColumns(datasource: Datasource) { return datasource } +export function isEditableColumn(column: FieldSchema) { + const isAutoColumn = + column.autocolumn && column.autoReason !== AutoReason.FOREIGN_KEY + const isFormula = column.type === FieldTypes.FORMULA + return !(isAutoColumn || isFormula) +} + export function checkExternalTableSchemas(datasource: Datasource) { return checkForeignKeysAreAutoColumns(datasource) }