From cdf93e5e955c9ebef3fbef6d5da12939fabf8038 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 26 Nov 2024 13:11:32 +0000 Subject: [PATCH] fix: use alias when constructing hm filter query to avoid self-reference issue --- packages/nocodb/src/db/conditionV2.ts | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/packages/nocodb/src/db/conditionV2.ts b/packages/nocodb/src/db/conditionV2.ts index dce45a2b56..7c8c0b9beb 100644 --- a/packages/nocodb/src/db/conditionV2.ts +++ b/packages/nocodb/src/db/conditionV2.ts @@ -213,30 +213,18 @@ const parseConditionV2 = async ( filter.comparison_op, ) ) { - // handle self reference - if (parentModel.id === childModel.id) { - if (filter.comparison_op === 'blank') { - return (qb) => { - qb.whereNull(childColumn.column_name); - }; - } else { - return (qb) => { - qb.whereNotNull(childColumn.column_name); - }; - } - } + const childTableAlias = getAlias(aliasCount); const selectHmCount = knex( - baseModelSqlv2.getTnPath(childModel.table_name), + baseModelSqlv2.getTnPath(childModel.table_name, childTableAlias), ) .count(childColumn.column_name) - .where( + .whereRaw('??.?? = ??.??', [ + childTableAlias, childColumn.column_name, - knex.raw('??.??', [ - alias || baseModelSqlv2.getTnPath(parentModel.table_name), - parentColumn.column_name, - ]), - ); + alias || baseModelSqlv2.getTnPath(parentModel.table_name), + parentColumn.column_name, + ]); return (qb) => { if (filter.comparison_op === 'blank') {