mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fix: incorrect sorting of entities with multi-inheritances (#9406)
* Fixed sorting of entities with multi-inheritances * prettier * added test case --------- Co-authored-by: Alex Messer <dmzt08@gmail.com>
This commit is contained in:
parent
adce6985d8
commit
54ca9dd801
@ -69,8 +69,9 @@ export class SubjectTopoligicalSorter {
|
||||
const entityTargetSubjects = this.subjects.filter(
|
||||
(subject) =>
|
||||
subject.metadata.targetName === sortedEntityTarget ||
|
||||
subject.metadata.parentEntityMetadata?.targetName ===
|
||||
sortedEntityTarget,
|
||||
subject.metadata.inheritanceTree.some(
|
||||
(s) => s.name === sortedEntityTarget,
|
||||
),
|
||||
)
|
||||
sortedSubjects.push(...entityTargetSubjects)
|
||||
this.removeAlreadySorted(entityTargetSubjects)
|
||||
|
||||
29
test/github-issues/9405/entity/Change.ts
Normal file
29
test/github-issues/9405/entity/Change.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import {
|
||||
Column,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
} from "../../../../src/index.js"
|
||||
import { ChangeLog } from "./ChangeLog.js"
|
||||
|
||||
@Entity()
|
||||
export class Change<T> {
|
||||
@PrimaryGeneratedColumn("increment")
|
||||
id: number
|
||||
|
||||
@Column("varchar", { nullable: false, length: 255 })
|
||||
propertyName: string
|
||||
|
||||
@Column("json", { nullable: true })
|
||||
oldValue?: any
|
||||
|
||||
@Column("json", { nullable: true })
|
||||
newValue?: any
|
||||
|
||||
@ManyToOne(() => ChangeLog, {
|
||||
cascade: false,
|
||||
nullable: false,
|
||||
onDelete: "CASCADE",
|
||||
})
|
||||
public log: ChangeLog<T>
|
||||
}
|
||||
9
test/github-issues/9405/entity/ChangeLog.ts
Normal file
9
test/github-issues/9405/entity/ChangeLog.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { OneToMany, ChildEntity } from "../../../../src/index.js"
|
||||
import { Change } from "./Change.js"
|
||||
import { Log } from "./Log.js"
|
||||
|
||||
@ChildEntity()
|
||||
export abstract class ChangeLog<T> extends Log {
|
||||
@OneToMany(() => Change, (change) => change.log, { cascade: true })
|
||||
changes: Change<T>[]
|
||||
}
|
||||
7
test/github-issues/9405/entity/EmailChanged.ts
Normal file
7
test/github-issues/9405/entity/EmailChanged.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { ChildEntity } from "../../../../src/index.js"
|
||||
import { ChangeLog } from "./ChangeLog.js"
|
||||
|
||||
export class Email {}
|
||||
|
||||
@ChildEntity()
|
||||
export class EmailChanged extends ChangeLog<Email> {}
|
||||
12
test/github-issues/9405/entity/Log.ts
Normal file
12
test/github-issues/9405/entity/Log.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
TableInheritance,
|
||||
} from "../../../../src/index.js"
|
||||
|
||||
@Entity()
|
||||
@TableInheritance({ column: { type: "varchar", name: "type" } })
|
||||
export abstract class Log {
|
||||
@PrimaryGeneratedColumn("increment")
|
||||
id: number
|
||||
}
|
||||
35
test/github-issues/9405/issue-9405.ts
Normal file
35
test/github-issues/9405/issue-9405.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import "reflect-metadata"
|
||||
import {
|
||||
closeTestingConnections,
|
||||
createTestingConnections,
|
||||
reloadTestingDatabases,
|
||||
} from "../../utils/test-utils"
|
||||
import { DataSource } from "../../../src/index.js"
|
||||
import { Email, EmailChanged } from "./entity/EmailChanged.js"
|
||||
import { Change } from "./entity/Change.js"
|
||||
import { Log } from "./entity/Log.js"
|
||||
|
||||
describe("github issues > #9405 Incorrect subject sorting with multi-inheritance entities", () => {
|
||||
let dataSources: DataSource[]
|
||||
before(
|
||||
async () =>
|
||||
(dataSources = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
enabledDrivers: ["postgres"],
|
||||
})),
|
||||
)
|
||||
beforeEach(() => reloadTestingDatabases(dataSources))
|
||||
after(() => closeTestingConnections(dataSources))
|
||||
|
||||
it("should correctly sort entities with multi-inheritances", () =>
|
||||
Promise.all(
|
||||
dataSources.map(async (dataSource) => {
|
||||
const emailChanged = new EmailChanged()
|
||||
const change = new Change<Email>()
|
||||
change.propertyName = "Example"
|
||||
emailChanged.changes = [change]
|
||||
|
||||
await dataSource.getRepository(Log).save(emailChanged)
|
||||
}),
|
||||
))
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user