From 5373b867e155cb91b5fcd441d8e3ab5000a0a583 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 22 Jun 2023 12:32:39 +0100 Subject: [PATCH 1/2] Fix for #10949 - the relationship types were always locked to be the same, which means that the foreign key wasn't being updated correctly as the backend was not aware of which side was the one-to-many side. --- .../Datasources/CreateEditRelationship.svelte | 12 ++++++++++- .../server/src/api/controllers/row/utils.ts | 20 +++++++++++++++++-- yarn.lock | 19 ++++++++++++++++-- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index 1413cd157e..4908512515 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -59,7 +59,6 @@ $: valid = getErrorCount(errors) === 0 && allRequiredAttributesSet() $: isManyToMany = relationshipType === RelationshipTypes.MANY_TO_MANY $: isManyToOne = relationshipType === RelationshipTypes.MANY_TO_ONE - $: toRelationship.relationshipType = fromRelationship?.relationshipType function getTable(id) { return plusTables.find(table => table._id === id) @@ -180,6 +179,16 @@ return getErrorCount(errors) === 0 } + function otherRelationshipType(type) { + if (type === RelationshipTypes.MANY_TO_ONE) { + return RelationshipTypes.ONE_TO_MANY + } else if (type === RelationshipTypes.ONE_TO_MANY) { + return RelationshipTypes.MANY_TO_ONE + } else if (type === RelationshipTypes.MANY_TO_MANY) { + return RelationshipTypes.MANY_TO_MANY + } + } + function buildRelationships() { const id = Helpers.uuid() //Map temporary variables @@ -200,6 +209,7 @@ ...toRelationship, tableId: fromId, name: fromColumn, + relationshipType: otherRelationshipType(relationshipType), through: throughId, type: "link", _id: id, diff --git a/packages/server/src/api/controllers/row/utils.ts b/packages/server/src/api/controllers/row/utils.ts index f1edbf538b..7309f0b7a5 100644 --- a/packages/server/src/api/controllers/row/utils.ts +++ b/packages/server/src/api/controllers/row/utils.ts @@ -3,10 +3,10 @@ import * as userController from "../user" import { FieldTypes } from "../../../constants" import { context } from "@budibase/backend-core" import { makeExternalQuery } from "../../../integrations/base/query" -import { Row, Table } from "@budibase/types" +import { FieldType, Row, Table, UserCtx } from "@budibase/types" import { Format } from "../view/exporters" -import { UserCtx } from "@budibase/types" import sdk from "../../../sdk" + const validateJs = require("validate.js") const { cloneDeep } = require("lodash/fp") @@ -20,6 +20,18 @@ validateJs.extend(validateJs.validators.datetime, { }, }) +function isForeignKey(key: string, table: Table) { + const relationships = Object.values(table.schema).filter( + column => column.type === FieldType.LINK + ) + for (let relationship of relationships) { + if (relationship.foreignKey === key) { + return true + } + } + return false +} + export async function getDatasourceAndQuery(json: any) { const datasourceId = json.endpoint.datasourceId const datasource = await sdk.datasources.get(datasourceId) @@ -65,6 +77,10 @@ export async function validate({ const column = fetchedTable.schema[fieldName] const constraints = cloneDeep(column.constraints) const type = column.type + // foreign keys are likely to be enriched + if (isForeignKey(fieldName, fetchedTable)) { + continue + } // formulas shouldn't validated, data will be deleted anyway if (type === FieldTypes.FORMULA || column.autocolumn) { continue diff --git a/yarn.lock b/yarn.lock index 9a57eb4377..981d17e96a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2489,6 +2489,11 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" +"@fontsource/source-sans-pro@^5.0.3": + version "5.0.3" + resolved "https://registry.yarnpkg.com/@fontsource/source-sans-pro/-/source-sans-pro-5.0.3.tgz#7d6e84a8169ba12fa5e6ce70757aa2ca7e74d855" + integrity sha512-mQnjuif/37VxwRloHZ+wQdoozd2VPWutbFSt1AuSkk7nFXIBQxHJLw80rgCF/osL0t7N/3Gx1V7UJuOX2zxzhQ== + "@fortawesome/fontawesome-common-types@6.3.0": version "6.3.0" resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-6.3.0.tgz#51f734e64511dbc3674cd347044d02f4dd26e86b" @@ -8408,7 +8413,7 @@ chmodr@1.2.0: resolved "https://registry.yarnpkg.com/chmodr/-/chmodr-1.2.0.tgz#720e96caa09b7f1cdbb01529b7d0ab6bc5e118b9" integrity sha512-Y5uI7Iq/Az6HgJEL6pdw7THVd7jbVOTPwsmcPOBjQL8e3N+pz872kzK5QxYGEy21iRys+iHWV0UZQXDFJo1hyA== -chokidar@3.5.3, chokidar@^3.0.0, chokidar@^3.5.1, chokidar@^3.5.2: +chokidar@3.5.3, chokidar@^3.0.0, chokidar@^3.5.1, chokidar@^3.5.2, chokidar@^3.5.3: version "3.5.3" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd" integrity sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw== @@ -11852,7 +11857,7 @@ fast-glob@3.2.7: merge2 "^1.3.0" micromatch "^4.0.4" -fast-glob@^3.0.3: +fast-glob@^3.0.3, fast-glob@^3.2.11: version "3.2.12" resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.12.tgz#7f39ec99c2e6ab030337142da9e0c18f37afae80" integrity sha512-DVj4CQIYYow0BlaelwK1pHl5n5cRSJfM60UA0zK891sVInoPri2Ekj7+e1CT3/3qxXenpI+nBBmQAcJPJgaj4w== @@ -25402,6 +25407,16 @@ vite-node@0.29.8: picocolors "^1.0.0" vite "^3.0.0 || ^4.0.0" +vite-plugin-static-copy@^0.16.0: + version "0.16.0" + resolved "https://registry.yarnpkg.com/vite-plugin-static-copy/-/vite-plugin-static-copy-0.16.0.tgz#2f65227037f17fc99c0782fd0b344e962935e69e" + integrity sha512-dMVEg5Z2SwYRgQnHZaeokvSKB4p/TOTf65JU4sP3U6ccSBsukqdtDOjpmT+xzTFHAA8WJjcS31RMLjUdWQCBzw== + dependencies: + chokidar "^3.5.3" + fast-glob "^3.2.11" + fs-extra "^11.1.0" + picocolors "^1.0.0" + "vite@^3.0.0 || ^4.0.0": version "4.2.2" resolved "https://registry.yarnpkg.com/vite/-/vite-4.2.2.tgz#014c30e5163844f6e96d7fe18fbb702236516dc6" From 58d7293798f65c563938108cbd89042619b8c434 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 22 Jun 2023 13:25:37 +0100 Subject: [PATCH 2/2] PR suggestion. --- packages/server/src/api/controllers/row/utils.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/server/src/api/controllers/row/utils.ts b/packages/server/src/api/controllers/row/utils.ts index 7309f0b7a5..a213f14a08 100644 --- a/packages/server/src/api/controllers/row/utils.ts +++ b/packages/server/src/api/controllers/row/utils.ts @@ -24,12 +24,7 @@ function isForeignKey(key: string, table: Table) { const relationships = Object.values(table.schema).filter( column => column.type === FieldType.LINK ) - for (let relationship of relationships) { - if (relationship.foreignKey === key) { - return true - } - } - return false + return relationships.some(relationship => relationship.foreignKey === key) } export async function getDatasourceAndQuery(json: any) {