From b072e93ad60435fc078f93d9a307ea59a34b693b Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 12 Sep 2024 09:38:54 +0530 Subject: [PATCH] fix: Post update extract new id since composite key can be changed in update (#9453) * fix: post update extract new id since composite key can be changed in update * fix: update bulkupdate --- packages/nocodb/src/db/BaseModelSqlv2.ts | 35 +++++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index a5c9c13d9a..a8b3137cf2 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -4790,12 +4790,16 @@ class BaseModelSqlv2 { await this.execAndParse(query, null, { raw: true }); - // const newData = await this.readByPk(id, false, {}, { ignoreView: true , getHiddenColumn: true}); - - // const prevData = await this.readByPk(id); + const newId = this.extractPksValues( + { + ...prevData, + ...updateObj, + }, + true, + ); const newData = await this.readByPk( - id, + newId, false, {}, { ignoreView: true, getHiddenColumn: true }, @@ -4804,7 +4808,7 @@ class BaseModelSqlv2 { if (btColumn && Object.keys(data || {}).length === 1) { await this.addChild({ colId: btColumn.id, - rowId: id, + rowId: newId, childId: updateObj[btForeignKeyColumn.title], cookie, onlyUpdateAuditLogs: true, @@ -5658,10 +5662,16 @@ class BaseModelSqlv2 { prevData.push(oldRecord); } - for (const { pk, data } of tempToRead) { + for (let i = 0; i < tempToRead.length; i++) { + const { pk, data } = tempToRead[i]; const wherePk = await this._wherePk(pk, true); toBeUpdated.push({ d: data, wherePk }); - updatePkValues.push(pk); + updatePkValues.push( + getCompositePkValue(this.model.primaryKeys, { + ...prevData[i], + ...data, + }), + ); } } } else { @@ -5671,7 +5681,12 @@ class BaseModelSqlv2 { toBeUpdated.push({ d, wherePk }); - updatePkValues.push(pkValues); + updatePkValues.push( + getCompositePkValue(this.model.primaryKeys, { + ...pkValues, + ...d, + }), + ); } } @@ -6469,7 +6484,7 @@ class BaseModelSqlv2 { protected async errorUpdate(_e, _data, _trx, _cookie) {} // todo: handle composite primary key - public extractPksValues(data: any) { + public extractPksValues(data: any, asString = false) { // data can be still inserted without PK // if composite primary key return an object with all the primary keys @@ -6478,7 +6493,7 @@ class BaseModelSqlv2 { for (const pk of this.model.primaryKeys) { pkValues[pk.title] = data[pk.title] ?? data[pk.column_name]; } - return pkValues; + return asString ? Object.values(pkValues).join('___') : pkValues; } else if (this.model.primaryKey) { return ( data[this.model.primaryKey.title] ??