Skip to content

Commit

Permalink
fix(rename decorator): properly map relation when renaming fk field
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-moncel committed Nov 27, 2024
1 parent da282e2 commit 2364d13
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2364d13

Please sign in to comment.