diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/CreateEditRelationship/CreateEditRelationship.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/CreateEditRelationship/CreateEditRelationship.svelte
index a30c08c13..902d8fa4e 100644
--- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/CreateEditRelationship/CreateEditRelationship.svelte
+++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/CreateEditRelationship/CreateEditRelationship.svelte
@@ -8,10 +8,10 @@
export let from
export let plusTables
export let relationship = {}
+ export let close
let originalName = relationship.name
- $: console.log(relationship)
$: tableOptions = plusTables.map(table => ({ label: table.name, value: table._id }))
$: valid = relationship.name && relationship.tableId && relationship.relationshipType
$: from = plusTables.find(table => table._id === relationship.source)
@@ -19,18 +19,6 @@
$: through = plusTables.find(table => table._id === relationship.through)
$: linkTable = through || to
- $: relationshipOptions = from && to ? [
- {
- name: `Many ${from.name} rows → many ${to.name} rows`,
- alt: `Many ${from.name} rows → many ${to.name} rows`,
- value: RelationshipTypes.MANY_TO_MANY,
- },
- {
- name: `One ${from.name} row → many ${to.name} rows`,
- alt: `One ${from.name} rows → many ${to.name} rows`,
- value: RelationshipTypes.ONE_TO_MANY,
- }
- ] : []
$: relationshipTypes = [
{
@@ -51,24 +39,43 @@
// save the relationship on to the datasource
async function saveRelationship() {
+ const manyToMany = relationship.relationshipType === RelationshipTypes.MANY_TO_MANY
// source of relationship
datasource.entities[from.name].schema[relationship.name] = {
type: "link",
...relationship
}
- // if (originalName !== from.name) {
- // delete datasource.entities[from.name].schema[originalName]
- // }
-
// save other side of relationship in the other schema
datasource.entities[to.name].schema[relationship.name] = {
+ name: relationship.name,
type: "link",
- relationshipType: relationship.relationshipType === RelationshipTypes.MANY_TO_MANY ? RelationshipTypes.MANY_TO_MANY : RelationshipTypes.MANY_TO_ONE,
- tableId: to._id
+ relationshipType: manyToMany ? RelationshipTypes.MANY_TO_MANY : RelationshipTypes.MANY_TO_ONE,
+ tableId: from._id,
+ fieldName: relationship.fieldName,
+ foreignKey: relationship.foreignKey
+ }
+
+ // If relationship has been renamed
+ if (originalName !== relationship.name) {
+ delete datasource.entities[from.name].schema[originalName]
+ delete datasource.entities[to.name].schema[originalName]
}
+ console.log({
+ from: datasource.entities[from.name].schema[relationship.name],
+ to: datasource.entities[to.name].schema[relationship.name],
+ })
+
+ await save()
+ await tables.fetch()
+ }
+
+ async function deleteRelationship() {
+ delete datasource.entities[from.name].schema[relationship.name]
+ delete datasource.entities[to.name].schema[relationship.name]
await save()
await tables.fetch()
+ close()
}
@@ -108,71 +115,19 @@
/>
{/if}
- {#if relationship?.relationshipType === RelationshipTypes.ONE_TO_MANY}
+ {#if relationship?.relationshipType === RelationshipTypes.ONE_TO_MANY && to}
{/if}
+
-
-
- {#if from && to}
-
-
-
-
-
- {/if}
+
+ {#if originalName !== null}
+
+ {/if}
diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/index.svelte
index 1b693c80d..91af7fab3 100644
--- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/index.svelte
+++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/index.svelte
@@ -2,6 +2,7 @@
import { goto, beforeUrlChange } from "@roxi/routify"
import { Button, Heading, Body, Divider, Layout, Modal } from "@budibase/bbui"
import { datasources, integrations, queries, tables } from "stores/backend"
+ import { RelationshipTypes } from "constants/backend"
import { notifications } from "@budibase/bbui"
import IntegrationConfigForm from "components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte"
import CreateEditRelationship from "./CreateEditRelationship/CreateEditRelationship.svelte"
@@ -66,7 +67,9 @@
}
function openRelationshipModal(relationship) {
- selectedRelationship = relationship
+ if (relationship.type === "link") {
+ selectedRelationship = relationship
+ }
relationshipModal.show()
}
@@ -82,7 +85,7 @@
-
+
{#if datasource && integration}
@@ -151,7 +154,7 @@
{#each plusTables as table}
{#each Object.keys(table.schema) as column}
- {#if table.schema[column].type === "link"}
+ {#if table.schema[column].type === "link" && table.schema[column].relationshipType !== RelationshipTypes.MANY_TO_ONE}
openRelationshipModal(table.schema[column])}>
diff --git a/packages/server/.vscode/launch.json b/packages/server/.vscode/launch.json
index d1774752c..ae5dda74e 100644
--- a/packages/server/.vscode/launch.json
+++ b/packages/server/.vscode/launch.json
@@ -13,15 +13,6 @@
"preLaunchTask": "npm: build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
},
- {
- "name": "TS",
- "type": "node",
- "request": "launch",
- "runtimeExecutable": "node",
- "runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
- "args": ["src/index.ts"],
- "cwd": "${workspaceRoot}",
- },
{
"type": "node",
"request": "launch",
diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts
index 31bd24440..e04a768d3 100644
--- a/packages/server/src/integrations/postgres.ts
+++ b/packages/server/src/integrations/postgres.ts
@@ -135,7 +135,10 @@ module PostgresModule {
* Fetches the tables from the postgres table and assigns them to the datasource.
* @param {*} datasourceId - datasourceId to fetch
*/
- async buildSchema(datasourceId: string) {
+ async buildSchema(
+ datasourceId: string,
+ entities: Record
+ ) {
let tableKeys: { [key: string]: string[] } = {}
try {
const primaryKeysResponse = await this.client.query(
@@ -167,6 +170,20 @@ module PostgresModule {
name: tableName,
schema: {},
}
+
+ // add the existing relationships from the entities if they exist, to prevent them from being overridden
+ if (entities) {
+ const existingTableSchema = entities[tableName].schema
+ for (let key in existingTableSchema) {
+ if (existingTableSchema[key].type === "link") {
+ tables[tableName].schema[key] = existingTableSchema[key]
+ }
+ }
+ console.log({
+ existingTableSchema,
+ tables
+ })
+ }
}
const type: string = convertType(column.data_type, TYPE_MAP)