diff --git a/packages/server/src/api/controllers/table/utils.ts b/packages/server/src/api/controllers/table/utils.ts index 96c01a15b8..743cce410b 100644 --- a/packages/server/src/api/controllers/table/utils.ts +++ b/packages/server/src/api/controllers/table/utils.ts @@ -15,7 +15,7 @@ import { getViews, saveView } from "../view/utils" import viewTemplate from "../view/viewBuilder" import { cloneDeep } from "lodash/fp" import { quotas } from "@budibase/pro" -import { context, events, features } from "@budibase/backend-core" +import { context, events, features, HTTPError } from "@budibase/backend-core" import { AutoFieldSubType, Database, @@ -145,14 +145,21 @@ export async function importToRows( // the real schema of the table passed in, not the clone used for // incrementing auto IDs for (const [fieldName, schema] of Object.entries(originalTable.schema)) { - const rowVal = Array.isArray(row[fieldName]) - ? row[fieldName] - : [row[fieldName]] + if (schema.type === FieldType.LINK) { + throw new HTTPError( + `Can't bulk import relationship fields for internal databases, found value in field "${fieldName}"`, + 400 + ) + } + if ( (schema.type === FieldType.OPTIONS || schema.type === FieldType.ARRAY) && row[fieldName] ) { + const rowVal = Array.isArray(row[fieldName]) + ? row[fieldName] + : [row[fieldName]] let merged = [...schema.constraints!.inclusion!, ...rowVal] let superSet = new Set(merged) schema.constraints!.inclusion = Array.from(superSet) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index cf3c87c430..a100a386d9 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -1823,6 +1823,39 @@ describe.each([ expect(row.autoId).toEqual(3) }) + isInternal && + it("should reject bulkImporting relationship fields", async () => { + const table1 = await config.api.table.save(saveTableRequest()) + const table2 = await config.api.table.save( + saveTableRequest({ + schema: { + relationship: { + name: "relationship", + type: FieldType.LINK, + tableId: table1._id!, + relationshipType: RelationshipType.ONE_TO_MANY, + fieldName: "relationship", + }, + }, + }) + ) + + const table1Row1 = await config.api.row.save(table1._id!, {}) + await config.api.row.bulkImport( + table2._id!, + { + rows: [{ relationship: [table1Row1._id!] }], + }, + { + status: 400, + body: { + message: + 'Can\'t bulk import relationship fields for internal databases, found value in field "relationship"', + }, + } + ) + }) + it("should be able to bulkImport rows", async () => { const table = await config.api.table.save( saveTableRequest({