From be0ab2c3d1dff98e4bc18e6fbcafe55c797eaac7 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 19 Apr 2022 11:51:33 +0530 Subject: [PATCH 1/2] fix: refer parent table referenced column in filter query(BT) re #1756 Signed-off-by: Pranav C --- .../src/lib/dataMapper/lib/sql/conditionV2.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/nocodb/src/lib/dataMapper/lib/sql/conditionV2.ts b/packages/nocodb/src/lib/dataMapper/lib/sql/conditionV2.ts index a4b34e4e53..3f2152c0c3 100644 --- a/packages/nocodb/src/lib/dataMapper/lib/sql/conditionV2.ts +++ b/packages/nocodb/src/lib/dataMapper/lib/sql/conditionV2.ts @@ -110,7 +110,7 @@ const parseConditionV2 = async ( }; } else if (colOptions.type === RelationTypes.BELONGS_TO) { const selectQb = knex(parentModel.table_name).select( - childColumn.column_name + parentColumn.column_name ); ( await parseConditionV2( @@ -323,7 +323,7 @@ async function generateLookupCondition( const parentModel = await parentColumn.getModel(); await parentModel.getColumns(); - if (relationColumnOptions.type === 'hm') { + if (relationColumnOptions.type === RelationTypes.HAS_MANY) { qb = knex(`${childModel.table_name} as ${alias}`); qb.select(`${alias}.${childColumn.column_name}`); @@ -347,7 +347,7 @@ async function generateLookupCondition( qbP.whereNotIn(parentColumn.column_name, qb); else qbP.whereIn(parentColumn.column_name, qb); }; - } else if (relationColumnOptions.type === 'bt') { + } else if (relationColumnOptions.type === RelationTypes.BELONGS_TO) { qb = knex(`${parentModel.table_name} as ${alias}`); qb.select(`${alias}.${childColumn.column_name}`); @@ -439,7 +439,7 @@ async function nestedConditionJoin( await parentModel.getColumns(); { switch (relationColOptions.type) { - case 'hm': + case RelationTypes.HAS_MANY: { qb.join( `${childModel.table_name} as ${relAlias}`, @@ -448,7 +448,7 @@ async function nestedConditionJoin( ); } break; - case 'bt': + case RelationTypes.BELONGS_TO: { qb.join( `${parentModel.table_name} as ${relAlias}`, @@ -492,7 +492,7 @@ async function nestedConditionJoin( ); } else { switch (relationColOptions.type) { - case 'hm': + case RelationTypes.HAS_MANY: { ( await parseConditionV2( @@ -508,7 +508,7 @@ async function nestedConditionJoin( )(qb); } break; - case 'bt': + case RelationTypes.BELONGS_TO: { ( await parseConditionV2( From 19a0eee9b41cd6303a16c4bc3b9aae5cb76d8040 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 19 Apr 2022 12:08:42 +0530 Subject: [PATCH 2/2] fix: wrap nested add update query select part to avoid issue when referencing same table Signed-off-by: Pranav C --- .../lib/dataMapper/lib/sql/BaseModelSqlv2.ts | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSqlv2.ts b/packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSqlv2.ts index a8d04e28cc..4130bf2ff9 100644 --- a/packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSqlv2.ts +++ b/packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSqlv2.ts @@ -1864,10 +1864,13 @@ class BaseModelSqlv2 { { await this.dbDriver(childTable.table_name) .update({ - [childColumn.column_name]: this.dbDriver(parentTable.table_name) - .select(parentColumn.column_name) - .where(_wherePk(parentTable.primaryKeys, rowId)) - .first() + [childColumn.column_name]: this.dbDriver.from( + this.dbDriver(parentTable.table_name) + .select(parentColumn.column_name) + .where(_wherePk(parentTable.primaryKeys, rowId)) + .first() + .as('___cn_alias') + ) }) .where(_wherePk(childTable.primaryKeys, childId)); } @@ -1876,10 +1879,13 @@ class BaseModelSqlv2 { { await this.dbDriver(childTable.table_name) .update({ - [childColumn.column_name]: this.dbDriver(parentTable.table_name) - .select(parentColumn.column_name) - .where(_wherePk(parentTable.primaryKeys, childId)) - .first() + [childColumn.column_name]: this.dbDriver.from( + this.dbDriver(parentTable.table_name) + .select(parentColumn.column_name) + .where(_wherePk(parentTable.primaryKeys, childId)) + .first() + .as('___cn_alias') + ) }) .where(_wherePk(childTable.primaryKeys, rowId)); }