From 83e8f75ab8b6af91d60653b3da8906a866cfce45 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Thu, 2 Mar 2023 15:35:35 +0800 Subject: [PATCH] fix(nocodb): handle self reference --- .../db/sql-data-mapper/lib/sql/conditionV2.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts index b117f2bce4..5e536d7bcd 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts @@ -91,6 +91,19 @@ const parseConditionV2 = async ( await parentModel.getColumns(); if (colOptions.type === RelationTypes.HAS_MANY) { if (['blank', 'notblank'].includes(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 selectHmCount = knex(childModel.table_name) .count(childColumn.column_name) .where( @@ -134,6 +147,19 @@ const parseConditionV2 = async ( }; } else if (colOptions.type === RelationTypes.BELONGS_TO) { if (['blank', 'notblank'].includes(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 selectBtCount = knex(parentModel.table_name) .count(parentColumn.column_name) .where( @@ -182,6 +208,19 @@ const parseConditionV2 = async ( const mmChildColumn = await colOptions.getMMChildColumn(); if (['blank', 'notblank'].includes(filter.comparison_op)) { + // handle self reference + if (mmModel.id === childModel.id) { + if (filter.comparison_op === 'blank') { + return (qb) => { + qb.whereNull(childColumn.column_name); + }; + } else { + return (qb) => { + qb.whereNotNull(childColumn.column_name); + }; + } + } + const selectMmCount = knex(mmModel.table_name) .count(mmChildColumn.column_name) .where(