Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Child entities not being saved correctly with cascade actions #6219

Merged
merged 2 commits into from Sep 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/metadata/EntityMetadata.ts
Expand Up @@ -700,14 +700,21 @@ export class EntityMetadata {
relations.forEach(relation => {
const value = relation.getEntityValue(entity);
if (Array.isArray(value)) {
value.forEach(subValue => relationsAndValues.push([relation, subValue, relation.inverseEntityMetadata]));
value.forEach(subValue => relationsAndValues.push([relation, subValue, this.getInverseEntityMetadata(subValue, relation)]));
} else if (value) {
relationsAndValues.push([relation, value, relation.inverseEntityMetadata]);
relationsAndValues.push([relation, value, this.getInverseEntityMetadata(value, relation)]);
}
});
return relationsAndValues;
}

private getInverseEntityMetadata(value: any, relation: RelationMetadata): EntityMetadata {
const childEntityMetadata = relation.inverseEntityMetadata.childEntityMetadatas.find(metadata =>
metadata.target === value.constructor
);
return childEntityMetadata ? childEntityMetadata : relation.inverseEntityMetadata;
}

// -------------------------------------------------------------------------
// Public Static Methods
// -------------------------------------------------------------------------
Expand Down