Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error when attempting to bulkImport a relationship field into an internal table. #14925

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/server/src/api/controllers/table/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions packages/server/src/api/routes/tests/row.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading