Merge pull request #14925 from Budibase/error-on-bulkimporting-relationship-fields

Throw an error when attempting to bulkImport a relationship field into an internal table.
This commit is contained in:
Sam Rose 2024-10-31 15:18:19 +00:00 committed by GitHub
commit 1f2301c0f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 4 deletions

View File

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

View File

@ -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({