From 1955c736851683a40c54ca38446f4c3c7265a0f0 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 12 Oct 2020 17:02:52 +0100 Subject: [PATCH] Handling empty relationship column the same way other columns are handled, it won't do anything until it is valid - but doesn't error. --- .../src/db/linkedRecords/LinkController.js | 10 +++++++++- packages/server/src/db/linkedRecords/index.js | 17 +++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/server/src/db/linkedRecords/LinkController.js b/packages/server/src/db/linkedRecords/LinkController.js index fc28320d70..48e459a1cb 100644 --- a/packages/server/src/db/linkedRecords/LinkController.js +++ b/packages/server/src/db/linkedRecords/LinkController.js @@ -234,8 +234,16 @@ class LinkController { for (let fieldName of Object.keys(schema)) { const field = schema[fieldName] if (field.type === "link") { + // handle this in a separate try catch, want + // the put to bubble up as an error, if can't update + // table for some reason + let linkedModel + try { + linkedModel = await this._db.get(field.modelId) + } catch (err) { + continue + } // create the link field in the other model - const linkedModel = await this._db.get(field.modelId) linkedModel.schema[field.fieldName] = { name: field.fieldName, type: "link", diff --git a/packages/server/src/db/linkedRecords/index.js b/packages/server/src/db/linkedRecords/index.js index ba93f3d2e8..09a58c1062 100644 --- a/packages/server/src/db/linkedRecords/index.js +++ b/packages/server/src/db/linkedRecords/index.js @@ -42,6 +42,7 @@ exports.updateLinks = async function({ model, oldModel, }) { + const baseReturnObj = record == null ? model : record if (instanceId == null) { throw "Cannot operate without an instance ID." } @@ -50,12 +51,16 @@ exports.updateLinks = async function({ arguments[0].modelId = model._id } let linkController = new LinkController(arguments[0]) - if ( - !(await linkController.doesModelHaveLinkedFields()) && - (oldModel == null || - !(await linkController.doesModelHaveLinkedFields(oldModel))) - ) { - return record + try { + if ( + !(await linkController.doesModelHaveLinkedFields()) && + (oldModel == null || + !(await linkController.doesModelHaveLinkedFields(oldModel))) + ) { + return baseReturnObj + } + } catch (err) { + return baseReturnObj } switch (eventType) { case EventType.RECORD_SAVE: