Browse Source

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
pull/9475/head
Pranav C 3 months ago committed by GitHub
parent
commit
b072e93ad6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 35
      packages/nocodb/src/db/BaseModelSqlv2.ts

35
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -4790,12 +4790,16 @@ class BaseModelSqlv2 {
await this.execAndParse(query, null, { raw: true }); await this.execAndParse(query, null, { raw: true });
// const newData = await this.readByPk(id, false, {}, { ignoreView: true , getHiddenColumn: true}); const newId = this.extractPksValues(
{
// const prevData = await this.readByPk(id); ...prevData,
...updateObj,
},
true,
);
const newData = await this.readByPk( const newData = await this.readByPk(
id, newId,
false, false,
{}, {},
{ ignoreView: true, getHiddenColumn: true }, { ignoreView: true, getHiddenColumn: true },
@ -4804,7 +4808,7 @@ class BaseModelSqlv2 {
if (btColumn && Object.keys(data || {}).length === 1) { if (btColumn && Object.keys(data || {}).length === 1) {
await this.addChild({ await this.addChild({
colId: btColumn.id, colId: btColumn.id,
rowId: id, rowId: newId,
childId: updateObj[btForeignKeyColumn.title], childId: updateObj[btForeignKeyColumn.title],
cookie, cookie,
onlyUpdateAuditLogs: true, onlyUpdateAuditLogs: true,
@ -5658,10 +5662,16 @@ class BaseModelSqlv2 {
prevData.push(oldRecord); 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); const wherePk = await this._wherePk(pk, true);
toBeUpdated.push({ d: data, wherePk }); toBeUpdated.push({ d: data, wherePk });
updatePkValues.push(pk); updatePkValues.push(
getCompositePkValue(this.model.primaryKeys, {
...prevData[i],
...data,
}),
);
} }
} }
} else { } else {
@ -5671,7 +5681,12 @@ class BaseModelSqlv2 {
toBeUpdated.push({ d, wherePk }); 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) {} protected async errorUpdate(_e, _data, _trx, _cookie) {}
// todo: handle composite primary key // todo: handle composite primary key
public extractPksValues(data: any) { public extractPksValues(data: any, asString = false) {
// data can be still inserted without PK // data can be still inserted without PK
// if composite primary key return an object with all the primary keys // 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) { for (const pk of this.model.primaryKeys) {
pkValues[pk.title] = data[pk.title] ?? data[pk.column_name]; pkValues[pk.title] = data[pk.title] ?? data[pk.column_name];
} }
return pkValues; return asString ? Object.values(pkValues).join('___') : pkValues;
} else if (this.model.primaryKey) { } else if (this.model.primaryKey) {
return ( return (
data[this.model.primaryKey.title] ?? data[this.model.primaryKey.title] ??

Loading…
Cancel
Save