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') {