diff --git a/packages/server/src/api/controllers/row/utils/sqlUtils.ts b/packages/server/src/api/controllers/row/utils/sqlUtils.ts index f28075bf84..e66a0b5bf6 100644 --- a/packages/server/src/api/controllers/row/utils/sqlUtils.ts +++ b/packages/server/src/api/controllers/row/utils/sqlUtils.ts @@ -166,7 +166,9 @@ export async function buildSqlFieldList( ) } else { table = source - fields = extractRealFields(source) + fields = extractRealFields(source).filter( + f => table.schema[f].visible !== false + ) } const containsFormula = (isView ? fields : Object.keys(table.schema)).some( diff --git a/packages/server/src/api/controllers/row/utils/tests/sqlUtils.spec.ts b/packages/server/src/api/controllers/row/utils/tests/sqlUtils.spec.ts new file mode 100644 index 0000000000..2ab8e251ea --- /dev/null +++ b/packages/server/src/api/controllers/row/utils/tests/sqlUtils.spec.ts @@ -0,0 +1,79 @@ +import { + AIOperationEnum, + FieldType, + RelationshipType, + Table, +} from "@budibase/types" +import { buildSqlFieldList } from "../sqlUtils" +import { structures } from "../../../../routes/tests/utilities" +import { cloneDeep } from "lodash" + +describe("buildSqlFieldList", () => { + const basicTable: Table = { + ...structures.basicTable(), + name: "table", + schema: { + name: { + type: FieldType.STRING, + name: "name", + }, + description: { + type: FieldType.STRING, + name: "description", + }, + amount: { + type: FieldType.NUMBER, + name: "amount", + }, + }, + } + + it("extracts fields from table schema", async () => { + const result = await buildSqlFieldList(basicTable, {}) + expect(result).toEqual(["table.name", "table.description", "table.amount"]) + }) + + it("excludes hidden fields", async () => { + const table = cloneDeep(basicTable) + table.schema.description.visible = false + const result = await buildSqlFieldList(table, {}) + expect(result).toEqual(["table.name", "table.amount"]) + }) + + it("excludes non-sql fields fields", async () => { + const table = cloneDeep(basicTable) + table.schema.formula = { + name: "formula", + type: FieldType.FORMULA, + formula: "any", + } + table.schema.ai = { + name: "ai", + type: FieldType.AI, + operation: AIOperationEnum.PROMPT, + } + table.schema.link = { + name: "link", + type: FieldType.LINK, + relationshipType: RelationshipType.ONE_TO_MANY, + fieldName: "link", + tableId: "otherTableId", + } + + const result = await buildSqlFieldList(table, {}) + expect(result).toEqual(["table.name", "table.description", "table.amount"]) + }) + + it("includes hidden fields if there is a formula column", async () => { + const table = cloneDeep(basicTable) + table.schema.description.visible = false + table.schema.formula = { + name: "formula", + type: FieldType.FORMULA, + formula: "any", + } + + const result = await buildSqlFieldList(table, {}) + expect(result).toEqual(["table.name", "table.description", "table.amount"]) + }) +})