(collection: string, field: string, opts?: MutationOptions)
| 695 | } |
| 696 | |
| 697 | async deleteField(collection: string, field: string, opts?: MutationOptions): Promise<void> { |
| 698 | if (this.accountability && this.accountability.admin !== true) { |
| 699 | throw new ForbiddenError(); |
| 700 | } |
| 701 | |
| 702 | const runPostColumnChange = await this.helpers.schema.preColumnChange(); |
| 703 | const nestedActionEvents: ActionEventParams[] = []; |
| 704 | |
| 705 | try { |
| 706 | if (opts?.emitEvents !== false) { |
| 707 | await emitter.emitFilter( |
| 708 | 'fields.delete', |
| 709 | [field], |
| 710 | { |
| 711 | collection: collection, |
| 712 | }, |
| 713 | { |
| 714 | database: this.knex, |
| 715 | schema: this.schema, |
| 716 | accountability: this.accountability, |
| 717 | }, |
| 718 | ); |
| 719 | } |
| 720 | |
| 721 | await transaction(this.knex, async (trx) => { |
| 722 | const relations = getRelations(this.schema.relations, collection, field); |
| 723 | |
| 724 | const relationsService = new RelationsService({ |
| 725 | knex: trx, |
| 726 | accountability: this.accountability, |
| 727 | schema: this.schema, |
| 728 | }); |
| 729 | |
| 730 | const fieldsService = new FieldsService({ |
| 731 | knex: trx, |
| 732 | accountability: this.accountability, |
| 733 | schema: this.schema, |
| 734 | }); |
| 735 | |
| 736 | for (const relation of relations) { |
| 737 | const isM2O = relation.collection === collection && relation.field === field; |
| 738 | |
| 739 | // If the current field is a m2o, delete the related o2m if it exists and remove the relationship |
| 740 | if (isM2O) { |
| 741 | await relationsService.deleteOne(collection, field, { |
| 742 | autoPurgeSystemCache: false, |
| 743 | bypassEmitAction: (params) => |
| 744 | opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params), |
| 745 | }); |
| 746 | |
| 747 | if ( |
| 748 | relation.related_collection && |
| 749 | relation.meta?.one_field && |
| 750 | relation.related_collection !== collection && |
| 751 | relation.meta.one_field !== field |
| 752 | ) { |
| 753 | await fieldsService.deleteField(relation.related_collection, relation.meta.one_field, { |
| 754 | autoPurgeCache: false, |
no test coverage detected