From 2f707794824b9c1dcf62efc61083c782b0e43434 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 21 Dec 2023 17:26:54 +0530 Subject: [PATCH] fix: has-many unlink api - accept array of object with primary key column Signed-off-by: Pranav C --- packages/nocodb/src/db/BaseModelSqlv2.ts | 43 +++++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index 5255844ad7..5043971602 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -5136,7 +5136,14 @@ class BaseModelSqlv2 { if (childRows.length !== childIds.length) { const missingIds = childIds.filter( (id) => - !childRows.find((r) => r[parentColumn.column_name] === id), + !childRows.find( + (r) => + r[parentColumn.column_name] === + (typeof id === 'object' + ? id[parentTable.primaryKey.title] || + id[parentTable.primaryKey.column_name] + : id), + ), ); NcError.unprocessableEntity( @@ -5175,9 +5182,28 @@ class BaseModelSqlv2 { { // validate Ids { - const childRowsQb = this.dbDriver(childTn) - .select(childTable.primaryKey.column_name) - .whereIn(childTable.primaryKey.column_name, childIds); + const childRowsQb = this.dbDriver(childTn).select( + childTable.primaryKey.column_name, + ); + + if (parentTable.primaryKeys.length > 1) { + childRowsQb.where((qb) => { + for (const childId of childIds) { + qb.orWhere(_wherePk(parentTable.primaryKeys, childId)); + } + }); + } else if (typeof childIds[0] === 'object') { + childRowsQb.whereIn( + parentTable.primaryKey.column_name, + childIds.map( + (c) => + c[parentTable.primaryKey.title] || + c[parentTable.primaryKey.column_name], + ), + ); + } else { + childRowsQb.whereIn(parentTable.primaryKey.column_name, childIds); + } const childRows = await this.execAndParse(childRowsQb, null, { raw: true, @@ -5186,7 +5212,14 @@ class BaseModelSqlv2 { if (childRows.length !== childIds.length) { const missingIds = childIds.filter( (id) => - !childRows.find((r) => r[parentColumn.column_name] === id), + !childRows.find( + (r) => + r[parentColumn.column_name] === + (typeof id === 'object' + ? id[parentTable.primaryKey.title] || + id[parentTable.primaryKey.column_name] + : id), + ), ); NcError.unprocessableEntity(