Merge pull request #1189 from Budibase/1-n-data-integrity
1 n data integrity
This commit is contained in:
commit
b2e9564a2c
|
@ -29,6 +29,12 @@
|
|||
// Prevent modal closing if there were errors
|
||||
return false
|
||||
}
|
||||
|
||||
if (rowResponse.status === 500) {
|
||||
notifier.danger(rowResponse.message)
|
||||
return false
|
||||
}
|
||||
|
||||
notifier.success("Row saved successfully.")
|
||||
backendUiStore.actions.rows.save(rowResponse)
|
||||
}
|
||||
|
|
|
@ -12,6 +12,11 @@ exports.FieldTypes = {
|
|||
AUTO: "auto",
|
||||
}
|
||||
|
||||
exports.RelationshipTypes = {
|
||||
ONE_TO_MANY: "one-to-many",
|
||||
MANY_TO_MANY: "many-to-many",
|
||||
}
|
||||
|
||||
exports.AuthTypes = {
|
||||
APP: "app",
|
||||
BUILDER: "builder",
|
||||
|
|
|
@ -2,7 +2,7 @@ const CouchDB = require("../index")
|
|||
const { IncludeDocs, getLinkDocuments } = require("./linkUtils")
|
||||
const { generateLinkID } = require("../utils")
|
||||
const Sentry = require("@sentry/node")
|
||||
const { FieldTypes } = require("../../constants")
|
||||
const { FieldTypes, RelationshipTypes } = require("../../constants")
|
||||
|
||||
/**
|
||||
* Creates a new link document structure which can be put to the database. It is important to
|
||||
|
@ -143,8 +143,30 @@ class LinkController {
|
|||
? linkDoc.doc2.rowId
|
||||
: linkDoc.doc1.rowId
|
||||
})
|
||||
|
||||
// if 1:N, ensure that this ID is not already attached to another record
|
||||
const linkedTable = await this._db.get(field.tableId)
|
||||
const linkedSchema = linkedTable.schema[field.fieldName]
|
||||
|
||||
// iterate through the link IDs in the row field, see if any don't exist already
|
||||
for (let linkId of rowField) {
|
||||
if (linkedSchema.relationshipType === RelationshipTypes.ONE_TO_MANY) {
|
||||
const links = await getLinkDocuments({
|
||||
appId: this._appId,
|
||||
tableId: field.tableId,
|
||||
rowId: linkId,
|
||||
includeDocs: IncludeDocs.INCLUDE,
|
||||
})
|
||||
|
||||
// The 1 side of 1:N is already related to something else
|
||||
// You must remove the existing relationship
|
||||
if (links.length > 0) {
|
||||
throw new Error(
|
||||
`1:N Relationship Error: Record already linked to another.`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (linkId && linkId !== "" && linkDocIds.indexOf(linkId) === -1) {
|
||||
// first check the doc we're linking to exists
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue