diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte index 384b87e11d..9b613c57e7 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/Relationships.svelte @@ -21,15 +21,21 @@ function getRelationships(tables) { const relatedColumns = {} - tables.forEach(({ name: tableName, schema }) => { + tables.forEach(({ name: tableName, schema, _id: tableId }) => { Object.values(schema).forEach(column => { if (column.type !== "link") return - relatedColumns[column._id] ??= {} - relatedColumns[column._id].through = - relatedColumns[column._id].through || column.through + const columnId = + column.through || + (column.main + ? `${tableId}_${column.fieldName}__${column.tableId}_${column.foreignKey}` + : `${column.tableId}_${column.foreignKey}__${tableId}_${column.fieldName}`) - relatedColumns[column._id][column.main ? "from" : "to"] = { + relatedColumns[columnId] ??= {} + relatedColumns[columnId].through = + relatedColumns[columnId].through || column.through + + relatedColumns[columnId][column.main ? "from" : "to"] = { ...column, tableName, }