diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte index 790a74df53..cda1ee2e26 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditRow.svelte @@ -28,10 +28,8 @@ .flat() // Prevent modal closing if there were errors return false - } - - if (rowResponse.status === 500) { - notifier.danger(rowResponse.message) + } else if (rowResponse.status === 400 || rowResponse.status === 500) { + errors = [{ message: rowResponse.message }] return false } diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditUser.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditUser.svelte index d5b242cc46..db92abc065 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditUser.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditUser.svelte @@ -60,7 +60,7 @@ .flat() } return false - } else if (rowResponse.status === 400 && rowResponse.message) { + } else if (rowResponse.status === 400 || rowResponse.status === 500) { errors = [{ message: rowResponse.message }] return false } diff --git a/packages/builder/src/components/common/LinkedRowSelector.svelte b/packages/builder/src/components/common/LinkedRowSelector.svelte index d470aa74de..5ac1efbc54 100644 --- a/packages/builder/src/components/common/LinkedRowSelector.svelte +++ b/packages/builder/src/components/common/LinkedRowSelector.svelte @@ -43,16 +43,6 @@ {:else} {#if schema.relationshipType === 'one-to-many'} - - {#each rows as row} - - {/each} - - {:else} + {:else} + + {#each rows as row} + + {/each} + {/if} {/if} diff --git a/packages/server/src/api/controllers/row.js b/packages/server/src/api/controllers/row.js index 413fedc6cd..5d783b7d18 100644 --- a/packages/server/src/api/controllers/row.js +++ b/packages/server/src/api/controllers/row.js @@ -7,6 +7,7 @@ const { DocumentTypes, SEPARATOR, ViewNames, + generateUserID, } = require("../../db/utils") const usersController = require("./user") const { @@ -140,7 +141,11 @@ exports.save = async function(ctx) { } if (!inputs._rev && !inputs._id) { - inputs._id = generateRowID(inputs.tableId) + if (inputs.tableId === ViewNames.USERS) { + inputs._id = generateUserID(inputs.email) + } else { + inputs._id = generateRowID(inputs.tableId) + } } // this returns the table and row incase they have been updated diff --git a/packages/server/src/db/linkedRows/LinkController.js b/packages/server/src/db/linkedRows/LinkController.js index b01d5eede5..a5404709f2 100644 --- a/packages/server/src/db/linkedRows/LinkController.js +++ b/packages/server/src/db/linkedRows/LinkController.js @@ -3,7 +3,6 @@ const { IncludeDocs, getLinkDocuments } = require("./linkUtils") const { generateLinkID } = require("../utils") const Sentry = require("@sentry/node") const { FieldTypes, RelationshipTypes } = require("../../constants") -const { isEqual } = require("lodash") /** * Creates a new link document structure which can be put to the database. It is important to @@ -133,6 +132,19 @@ class LinkController { } } + /** + * Returns whether the two schemas are equal (in the important parts, not a pure equality check) + */ + areSchemasEqual(schema1, schema2) { + const compareFields = ["name", "type", "tableId", "fieldName", "autocolumn"] + for (let field of compareFields) { + if (schema1[field] !== schema2[field]) { + return false + } + } + return true + } + // all operations here will assume that the table // this operation is related to has linked rows /** @@ -310,12 +322,28 @@ class LinkController { tableId: table._id, fieldName: fieldName, } + if (field.autocolumn) { linkConfig.autocolumn = field.autocolumn } + + if (field.relationshipType) { + // Ensure that the other side of the relationship is locked to one record + linkConfig.relationshipType = field.relationshipType + + // Update this table to be the many + table.schema[field.name].relationshipType = + RelationshipTypes.MANY_TO_MANY + const response = await this._db.put(table) + table._rev = response.rev + } + // check the linked table to make sure we aren't overwriting an existing column const existingSchema = linkedTable.schema[field.fieldName] - if (existingSchema != null && !isEqual(existingSchema, linkConfig)) { + if ( + existingSchema != null && + !this.areSchemasEqual(existingSchema, linkConfig) + ) { throw new Error("Cannot overwrite existing column.") } // create the link field in the other table