Create field in transaction

Fixes #4211
This commit is contained in:
rijkvanzanten
2021-03-04 17:35:39 -05:00
parent 61c5522c06
commit 5afa6c04fc

View File

@@ -217,23 +217,31 @@ export class FieldsService {
throw new InvalidPayloadException(`Field "${field.field}" already exists in collection "${collection}"`);
}
if (field.type && ALIAS_TYPES.includes(field.type) === false) {
if (table) {
this.addColumnToTable(table, field as Field);
} else {
await database.schema.alterTable(collection, (table) => {
await this.knex.transaction(async (trx) => {
const itemsService = new ItemsService('directus_fields', {
knex: trx,
accountability: this.accountability,
schema: this.schema,
});
if (field.type && ALIAS_TYPES.includes(field.type) === false) {
if (table) {
this.addColumnToTable(table, field as Field);
} else {
await trx.schema.alterTable(collection, (table) => {
this.addColumnToTable(table, field as Field);
});
}
}
if (field.meta) {
await itemsService.create({
...field.meta,
collection: collection,
field: field.field,
});
}
}
if (field.meta) {
await this.itemsService.create({
...field.meta,
collection: collection,
field: field.field,
});
}
});
if (cache && env.CACHE_AUTO_PURGE) {
await cache.clear();