diff --git a/packages/server/src/api/controllers/table/internal.ts b/packages/server/src/api/controllers/table/internal.ts index e468848c57..81e203d405 100644 --- a/packages/server/src/api/controllers/table/internal.ts +++ b/packages/server/src/api/controllers/table/internal.ts @@ -10,6 +10,7 @@ import { } from "../../../utilities/rowProcessor" import { runStaticFormulaChecks } from "./bulkFormula" import { + AutoColumnFieldMetadata, RenameColumn, SaveTableRequest, SaveTableResponse, @@ -35,7 +36,9 @@ function checkAutoColumns(table: Table, oldTable?: Table) { if (oldSchema && oldSchema.subtype) { table.schema[key].subtype = oldSchema.subtype } else { - table.schema[key] = fixAutoColumnSubType(schema) + table.schema[key] = fixAutoColumnSubType( + schema as AutoColumnFieldMetadata + ) } } return table @@ -78,10 +81,10 @@ export async function save(ctx: UserCtx) { // make sure that types don't change of a column, have to remove // the column if you want to change the type if (oldTable && oldTable.schema) { - for (let propKey of Object.keys(tableToSave.schema)) { + for (const propKey of Object.keys(tableToSave.schema)) { let oldColumn = oldTable.schema[propKey] if (oldColumn && oldColumn.type === FieldTypes.INTERNAL) { - oldColumn.type = FieldTypes.AUTO + oldColumn.type = FieldTypes.AUTO as any // TODO } } } diff --git a/packages/server/src/utilities/rowProcessor/utils.ts b/packages/server/src/utilities/rowProcessor/utils.ts index 0d7eace369..435174b4e7 100644 --- a/packages/server/src/utilities/rowProcessor/utils.ts +++ b/packages/server/src/utilities/rowProcessor/utils.ts @@ -5,13 +5,13 @@ import { FormulaTypes, } from "../../constants" import { processStringSync } from "@budibase/string-templates" -import { FieldSchema, Row, Table } from "@budibase/types" +import { AutoColumnFieldMetadata, Row, Table } from "@budibase/types" /** * If the subtype has been lost for any reason this works out what * subtype the auto column should be. */ -export function fixAutoColumnSubType(column: FieldSchema) { +export function fixAutoColumnSubType(column: AutoColumnFieldMetadata) { if (!column.autocolumn || !column.name || column.subtype) { return column } @@ -47,12 +47,14 @@ export function processFormulas( rowArray = rows } for (let [column, schema] of Object.entries(table.schema)) { - const isStatic = schema.formulaType === FormulaTypes.STATIC + const isStaticFormula = + schema.type === FieldTypes.FORMULA && + schema.formulaType === FormulaTypes.STATIC if ( schema.type !== FieldTypes.FORMULA || schema.formula == null || - (dynamic && isStatic) || - (!dynamic && !isStatic) + (dynamic && isStaticFormula) || + (!dynamic && !isStaticFormula) ) { continue } diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index 5e7fba25b9..1cdbb75303 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -34,8 +34,8 @@ export type RelationshipFieldMetadata = BaseFieldSchema & { export interface AutoColumnFieldMetadata extends BaseFieldSchema { type: FieldType.AUTO - autocolumn?: boolean - subtype?: string + autocolumn: true + subtype: AutoFieldSubTypes lastID?: number // if the column was turned to an auto-column for SQL, explains why (primary, foreign etc) autoReason?: AutoReason @@ -105,9 +105,25 @@ interface BaseFieldSchema extends UIFieldMetadata { // only used by external databases, to denote the real type externalType?: string constraints?: FieldConstraints + autocolumn?: boolean + subtype?: string +} + +interface OtherFieldMetadata extends BaseFieldSchema { + type: Exclude< + FieldType, + | FieldType.DATETIME + | FieldType.DATETIME + | FieldType.LINK + | FieldType.AUTO + | FieldType.STRING + | FieldType.FORMULA + | FieldType.NUMBER + > } export type FieldSchema = + | OtherFieldMetadata | DateFieldMetadata | RelationshipFieldMetadata | AutoColumnFieldMetadata