Fix deletions
This commit is contained in:
parent
96be447817
commit
00eedbf726
|
@ -11,6 +11,7 @@ import {
|
||||||
IncludeRelationship,
|
IncludeRelationship,
|
||||||
InternalSearchFilterOperator,
|
InternalSearchFilterOperator,
|
||||||
isManyToOne,
|
isManyToOne,
|
||||||
|
isOneToMany,
|
||||||
OneToManyRelationshipFieldMetadata,
|
OneToManyRelationshipFieldMetadata,
|
||||||
Operation,
|
Operation,
|
||||||
PaginationJson,
|
PaginationJson,
|
||||||
|
@ -50,6 +51,7 @@ import sdk from "../../../sdk"
|
||||||
import env from "../../../environment"
|
import env from "../../../environment"
|
||||||
import { makeExternalQuery } from "../../../integrations/base/query"
|
import { makeExternalQuery } from "../../../integrations/base/query"
|
||||||
import { dataFilters, helpers } from "@budibase/shared-core"
|
import { dataFilters, helpers } from "@budibase/shared-core"
|
||||||
|
import { isRelationshipColumn } from "../../../db/utils"
|
||||||
|
|
||||||
export interface ManyRelationship {
|
export interface ManyRelationship {
|
||||||
tableId?: string
|
tableId?: string
|
||||||
|
@ -606,25 +608,28 @@ export class ExternalRequest<T extends Operation> {
|
||||||
async removeRelationshipsToRow(table: Table, rowId: string) {
|
async removeRelationshipsToRow(table: Table, rowId: string) {
|
||||||
const row = await this.getRow(table, rowId)
|
const row = await this.getRow(table, rowId)
|
||||||
const related = await this.lookupRelations(table._id!, row)
|
const related = await this.lookupRelations(table._id!, row)
|
||||||
for (let column of Object.values(table.schema)) {
|
for (const column of Object.values(table.schema)) {
|
||||||
const relationshipColumn = column as RelationshipFieldMetadata
|
if (!isRelationshipColumn(column) || isOneToMany(column)) {
|
||||||
if (!isManyToOne(relationshipColumn)) {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const { rows, isMany, tableId } = related.find(
|
|
||||||
r => r.field === relationshipColumn.fieldName
|
const relatedByTable = isManyToMany(column)
|
||||||
)!
|
? related.find(
|
||||||
|
r => r.tableId === column.through && r.field === column.fieldName
|
||||||
|
)
|
||||||
|
: related.find(r => r.field === column.fieldName)
|
||||||
|
if (!relatedByTable) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
const { rows, isMany, tableId } = relatedByTable
|
||||||
const table = this.getTable(tableId)!
|
const table = this.getTable(tableId)!
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
rows.map(row => {
|
rows.map(row => {
|
||||||
const rowId = generateIdForRow(row, table)
|
const rowId = generateIdForRow(row, table)
|
||||||
return isMany
|
return isMany
|
||||||
? this.removeManyToManyRelationships(rowId, table)
|
? this.removeManyToManyRelationships(rowId, table)
|
||||||
: this.removeOneToManyRelationships(
|
: this.removeOneToManyRelationships(rowId, table, column.fieldName)
|
||||||
rowId,
|
|
||||||
table,
|
|
||||||
relationshipColumn.fieldName
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1542,6 +1542,72 @@ describe.each([
|
||||||
)
|
)
|
||||||
expect(res.length).toEqual(2)
|
expect(res.length).toEqual(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("relations to same table", () => {
|
||||||
|
let relatedRows: Row[]
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const relatedTable = await config.api.table.save(
|
||||||
|
defaultTable({
|
||||||
|
schema: {
|
||||||
|
name: { name: "name", type: FieldType.STRING },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
const relatedTableId = relatedTable._id!
|
||||||
|
table = await config.api.table.save(
|
||||||
|
defaultTable({
|
||||||
|
schema: {
|
||||||
|
name: { name: "name", type: FieldType.STRING },
|
||||||
|
related1: {
|
||||||
|
type: FieldType.LINK,
|
||||||
|
name: "related1",
|
||||||
|
fieldName: "main1",
|
||||||
|
tableId: relatedTableId,
|
||||||
|
relationshipType: RelationshipType.MANY_TO_MANY,
|
||||||
|
},
|
||||||
|
related2: {
|
||||||
|
type: FieldType.LINK,
|
||||||
|
name: "related2",
|
||||||
|
fieldName: "main2",
|
||||||
|
tableId: relatedTableId,
|
||||||
|
relationshipType: RelationshipType.MANY_TO_MANY,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
relatedRows = await Promise.all([
|
||||||
|
config.api.row.save(relatedTableId, { name: "foo" }),
|
||||||
|
config.api.row.save(relatedTableId, { name: "bar" }),
|
||||||
|
config.api.row.save(relatedTableId, { name: "baz" }),
|
||||||
|
config.api.row.save(relatedTableId, { name: "boo" }),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can delete rows with both relationships", async () => {
|
||||||
|
const row = await config.api.row.save(table._id!, {
|
||||||
|
name: "test",
|
||||||
|
related1: [relatedRows[0]._id!],
|
||||||
|
related2: [relatedRows[1]._id!],
|
||||||
|
})
|
||||||
|
|
||||||
|
await config.api.row.delete(table._id!, { _id: row._id! })
|
||||||
|
|
||||||
|
await config.api.row.get(table._id!, row._id!, { status: 404 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can delete rows with empty relationships", async () => {
|
||||||
|
const row = await config.api.row.save(table._id!, {
|
||||||
|
name: "test",
|
||||||
|
related1: [],
|
||||||
|
related2: [],
|
||||||
|
})
|
||||||
|
|
||||||
|
await config.api.row.delete(table._id!, { _id: row._id! })
|
||||||
|
|
||||||
|
await config.api.row.get(table._id!, row._id!, { status: 404 })
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("validate", () => {
|
describe("validate", () => {
|
||||||
|
|
Loading…
Reference in New Issue