From 2364d13d78c83f73f24a826ed226136afcdcc742 Mon Sep 17 00:00:00 2001 From: Arnaud Moncel Date: Tue, 26 Nov 2024 16:07:15 +0100 Subject: [PATCH] fix(rename decorator): properly map relation when renaming fk field --- .../src/decorators/rename-field/collection.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/datasource-customizer/src/decorators/rename-field/collection.ts b/packages/datasource-customizer/src/decorators/rename-field/collection.ts index 13fd4cea7..45596e4ae 100644 --- a/packages/datasource-customizer/src/decorators/rename-field/collection.ts +++ b/packages/datasource-customizer/src/decorators/rename-field/collection.ts @@ -59,14 +59,24 @@ export default class RenameFieldCollectionDecorator extends CollectionDecorator const schema = { ...oldSchema }; if (schema.type === 'ManyToOne') { + const relation = this.dataSource.getCollection(schema.foreignCollection); schema.foreignKey = this.fromChildCollection[schema.foreignKey] ?? schema.foreignKey; + schema.foreignKeyTarget = + relation.fromChildCollection[schema.foreignKeyTarget] ?? schema.foreignKeyTarget; } else if (schema.type === 'OneToMany' || schema.type === 'OneToOne') { const relation = this.dataSource.getCollection(schema.foreignCollection); schema.originKey = relation.fromChildCollection[schema.originKey] ?? schema.originKey; + schema.originKeyTarget = + this.fromChildCollection[schema.originKeyTarget] ?? schema.originKeyTarget; } else if (schema.type === 'ManyToMany') { const through = this.dataSource.getCollection(schema.throughCollection); + const relation = this.dataSource.getCollection(schema.foreignCollection); schema.foreignKey = through.fromChildCollection[schema.foreignKey] ?? schema.foreignKey; schema.originKey = through.fromChildCollection[schema.originKey] ?? schema.originKey; + schema.originKeyTarget = + this.fromChildCollection[schema.originKeyTarget] ?? schema.originKeyTarget; + schema.foreignKeyTarget = + relation.fromChildCollection[schema.foreignKeyTarget] ?? schema.foreignKeyTarget; } fields[this.fromChildCollection[oldName] ?? oldName] = schema;