From 21700a668ede087235cf27b06fa7d2fa5bd6cd5c Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Mon, 11 Aug 2025 16:25:46 -0400 Subject: [PATCH] add error checking for QueryService.leftJoin --- packages/backend/src/core/QueryService.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/core/QueryService.ts b/packages/backend/src/core/QueryService.ts index 1cca99d452..2fdea12fc8 100644 --- a/packages/backend/src/core/QueryService.ts +++ b/packages/backend/src/core/QueryService.ts @@ -285,7 +285,27 @@ export class QueryService { @bindThis public leftJoin(q: SelectQueryBuilder, relation: string, alias: string, condition?: string): SelectQueryBuilder { // Skip if it's already joined, otherwise we'll get an error - if (!q.expressionMap.joinAttributes.some(j => j.alias.name === alias)) { + const join = q.expressionMap.joinAttributes.find(j => j.alias.name === alias); + if (join) { + const oldRelation = typeof(join.entityOrProperty) === 'function' + ? join.entityOrProperty.name + : join.entityOrProperty; + + const oldQuery = join.condition + ? `JOIN ${oldRelation} AS ${alias} ON ${join.condition}` + : `JOIN ${oldRelation} AS ${alias}`; + const newQuery = condition + ? `JOIN ${relation} AS ${alias} ON ${oldRelation}` + : `JOIN ${relation} AS ${alias}`; + + if (oldRelation !== relation) { + throw new Error(`Query error: cannot add ${newQuery}: alias already used by ${oldQuery}`); + } + + if (join.condition !== condition) { + throw new Error(`Query error: cannot add ${newQuery}: relation already defined with different condition by ${oldQuery}`); + } + } else { q.leftJoin(relation, alias, condition); }