Always remove the primary key field from the update payload (#4576)

* Always remove the primary key field from the update payload to prevent issues with databases that don't allow certain primary key columns (e.g. mssql identity columns)

* Opinionated cleanup

Co-authored-by: Martijn Boland <martijn@taiga.nl>
Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
Martijn Boland
2021-03-18 03:14:08 +01:00
committed by GitHub
parent 2a71f0c0dd
commit aca08e24c7

View File

@@ -21,7 +21,7 @@ import env from '../env';
import { PayloadService } from './payload';
import { AuthorizationService } from './authorization';
import { pick, clone, cloneDeep, merge } from 'lodash';
import { pick, clone, cloneDeep, merge, without } from 'lodash';
import getDefaultValue from '../utils/get-default-value';
import { translateDatabaseError } from '../exceptions/database/translate';
import { InvalidPayloadException, ForbiddenException } from '../exceptions';
@@ -313,13 +313,15 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
payload = await payloadService.processM2O(payload);
payload = await payloadService.processA2O(payload);
let payloadWithoutAliases = pick(payload, columns);
let payloadWithoutAliasAndPK = pick(payload, without(columns, primaryKeyField));
payloadWithoutAliases = await payloadService.processValues('update', payloadWithoutAliases);
payloadWithoutAliasAndPK = await payloadService.processValues('update', payloadWithoutAliasAndPK);
if (Object.keys(payloadWithoutAliases).length > 0) {
console.log(payloadWithoutAliasAndPK);
if (Object.keys(payloadWithoutAliasAndPK).length > 0) {
try {
await trx(this.collection).update(payloadWithoutAliases).whereIn(primaryKeyField, keys);
await trx(this.collection).update(payloadWithoutAliasAndPK).whereIn(primaryKeyField, keys);
} catch (err) {
throw await translateDatabaseError(err);
}
@@ -364,7 +366,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
item: keys[index],
data:
snapshots && Array.isArray(snapshots) ? JSON.stringify(snapshots?.[index]) : JSON.stringify(snapshots),
delta: JSON.stringify(payloadWithoutAliases),
delta: JSON.stringify(payloadWithoutAliasAndPK),
}));
if (revisionRecords.length > 0) {