diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte index 5929b2db96..790a74df53 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte @@ -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) } diff --git a/packages/server/src/db/linkedRows/LinkController.js b/packages/server/src/db/linkedRows/LinkController.js index 96074c0ef2..4535cb0274 100644 --- a/packages/server/src/db/linkedRows/LinkController.js +++ b/packages/server/src/db/linkedRows/LinkController.js @@ -132,14 +132,6 @@ class LinkController { const rowField = row[fieldName] const field = table.schema[fieldName] if (field.type === FieldTypes.LINK && rowField != null) { - // 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] - - if (linkedSchema.relationshipType === "one-to-many") { - - } - // check which links actual pertain to the update in this row const thisFieldLinkDocs = linkDocs.filter( linkDoc => @@ -151,6 +143,31 @@ 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] + + if (linkedSchema.relationshipType === "one-to-many") { + for (let linkId of rowField) { + 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.` + ) + } + console.log("ONE TO MANY") + } + } + // iterate through the link IDs in the row field, see if any don't exist already for (let linkId of rowField) { if (linkId && linkId !== "" && linkDocIds.indexOf(linkId) === -1) {