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 2 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 });
// 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] ??

Loading…
Cancel
Save