mirror of
https://github.com/directus/directus.git
synced 2026-01-22 19:58:09 -05:00
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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user