Skip to content

Commit

Permalink
fix: Create correct children with STI and trees
Browse files Browse the repository at this point in the history
This commit fixes the `create` function for EntityManager and TreeRepository
to create entities of correct type when using Single Table Inheritance
and complex inheritance with Trees.

Related to: #7758
  • Loading branch information
felix-gohla committed Aug 22, 2022
1 parent a59e4bc commit b006420
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/metadata-builder/EntityMetadataBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,15 @@ export class EntityMetadataBuilder {
) {
entityMetadata.ownColumns.push(discriminatorColumn)
}
// also copy the inheritance pattern & tree metadata
// this comes in handy when inheritance and trees are used together
entityMetadata.inheritancePattern = entityMetadata.parentEntityMetadata.inheritancePattern
if (!entityMetadata.treeType && !!entityMetadata.parentEntityMetadata.treeType) {
entityMetadata.treeType = entityMetadata.parentEntityMetadata.treeType
entityMetadata.treeOptions = entityMetadata.parentEntityMetadata.treeOptions
entityMetadata.treeParentRelation = entityMetadata.parentEntityMetadata.treeParentRelation
entityMetadata.treeLevelColumn = entityMetadata.parentEntityMetadata.treeLevelColumn
}
}

const { namingStrategy } = this.connection
Expand Down
10 changes: 6 additions & 4 deletions src/metadata/EntityMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,16 +847,18 @@ export class EntityMetadata {
}

/**
* In the case of SingleTableInheritance, find the correct child metadata
* In the case of SingleTableInheritance, find the correct metadata
* for a given value.
*
* @param value The value to find the metadata for.
* @returns The found metadata for the child entity or the base metadata if no child was found.
* @returns The found metadata for the entity or the base metadata if no matching metadata
* was found in the whole inheritance tree.
*/
findChildMetadata(value: any): EntityMetadata {
findInheritanceMetadata(value: any): EntityMetadata {
// Check for single table inheritance and find the correct metadata in that case.
// Goal is to use the correct discriminator as we could have a repository
// for an (abstract) base class and thus the target would not match.

if (
this.inheritancePattern === "STI" &&
this.childEntityMetadatas.length > 0
Expand Down Expand Up @@ -887,7 +889,7 @@ export class EntityMetadata {
value: any,
relation: RelationMetadata,
): EntityMetadata {
return relation.inverseEntityMetadata.findChildMetadata(value)
return relation.inverseEntityMetadata.findInheritanceMetadata(value)
}

// -------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/persistence/EntityPersistExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class EntityPersistExecutor {

let metadata = this.connection
.getMetadata(entityTarget)
.findChildMetadata(entity)
.findInheritanceMetadata(entity)

subjects.push(
new Subject({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class PlainObjectToNewEntityTransformer {
})

const inverseEntityMetadata =
relation.inverseEntityMetadata.findChildMetadata(
relation.inverseEntityMetadata.findInheritanceMetadata(
objectRelatedValueItem,
)

Expand Down Expand Up @@ -115,7 +115,7 @@ export class PlainObjectToNewEntityTransformer {
}

const inverseEntityMetadata =
relation.inverseEntityMetadata.findChildMetadata(
relation.inverseEntityMetadata.findInheritanceMetadata(
objectRelatedValue,
)

Expand Down

0 comments on commit b006420

Please sign in to comment.