From cc18d6753590a4a3437cdfdb6089a1b663bdab39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Magrin?= Date: Wed, 26 Jun 2024 15:40:25 +0200 Subject: [PATCH] fix: avoid join column on both side --- .../twenty-orm/utils/get-join-column.util.ts | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/twenty-server/src/engine/twenty-orm/utils/get-join-column.util.ts b/packages/twenty-server/src/engine/twenty-orm/utils/get-join-column.util.ts index 5d47467887b1..e352719410b0 100644 --- a/packages/twenty-server/src/engine/twenty-orm/utils/get-join-column.util.ts +++ b/packages/twenty-server/src/engine/twenty-orm/utils/get-join-column.util.ts @@ -16,11 +16,29 @@ export const getJoinColumn = ( return null; } + const inverseSideTarget = relationMetadataArgs.inverseSideTarget(); + const inverseSideJoinColumnsMetadataArgsCollection = + metadataArgsStorage.filterJoinColumns(inverseSideTarget); const filteredJoinColumnsMetadataArgsCollection = joinColumnsMetadataArgsCollection.filter( (joinColumnsMetadataArgs) => joinColumnsMetadataArgs.relationName === relationMetadataArgs.name, ); + const oppositeFilteredJoinColumnsMetadataArgsCollection = + inverseSideJoinColumnsMetadataArgsCollection.filter( + (joinColumnsMetadataArgs) => + joinColumnsMetadataArgs.relationName === relationMetadataArgs.name, + ); + + if ( + relationMetadataArgs.type === RelationMetadataType.ONE_TO_ONE && + filteredJoinColumnsMetadataArgsCollection.length === 0 && + oppositeFilteredJoinColumnsMetadataArgsCollection.length === 0 + ) { + throw new Error( + `Join column for one-to-one ${relationMetadataArgs.name} relation is missing on both sides`, + ); + } // If we're in a ONE_TO_ONE relation and there are no join columns, we need to find the join column on the inverse side if ( @@ -28,9 +46,6 @@ export const getJoinColumn = ( filteredJoinColumnsMetadataArgsCollection.length === 0 && !opposite ) { - const inverseSideTarget = relationMetadataArgs.inverseSideTarget(); - const inverseSideJoinColumnsMetadataArgsCollection = - metadataArgsStorage.filterJoinColumns(inverseSideTarget); const inverseSideRelationMetadataArgsCollection = metadataArgsStorage.filterRelations(inverseSideTarget); const inverseSideRelationMetadataArgs = @@ -42,7 +57,7 @@ export const getJoinColumn = ( if (!inverseSideRelationMetadataArgs) { throw new Error( - `Inverse side relation metadata args are missing for relation ${relationMetadataArgs.name}`, + `Inverse side join column of relation ${relationMetadataArgs.name} is missing`, ); } @@ -57,7 +72,7 @@ export const getJoinColumn = ( // Check if there are multiple join columns for the relation if (filteredJoinColumnsMetadataArgsCollection.length > 1) { throw new Error( - `Multiple join columns metadata args found for relation ${relationMetadataArgs.name}`, + `Multiple join columns found for relation ${relationMetadataArgs.name}`, ); } @@ -65,7 +80,7 @@ export const getJoinColumn = ( if (!joinColumnsMetadataArgs) { throw new Error( - `Join columns metadata args are missing for relation ${relationMetadataArgs.name}`, + `Join column is missing for relation ${relationMetadataArgs.name}`, ); }