Skip to content

Commit

Permalink
fix: handle inherited relations insert order (#9242)
Browse files Browse the repository at this point in the history
Take inheritance into consideration when sorting insert commands

Closes: #9241

Co-authored-by: Kevin KONRAD <ext.kevin.konrad@reseau.sncf.fr>
  • Loading branch information
konradkevin and Kevin KONRAD committed Aug 24, 2022
1 parent e519910 commit 14dfadb
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/persistence/SubjectTopoligicalSorter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export class SubjectTopoligicalSorter {
// add those sorted targets and remove them from original array of targets
sortedNonNullableEntityTargets.forEach((sortedEntityTarget) => {
const entityTargetSubjects = this.subjects.filter(
(subject) => subject.metadata.targetName === sortedEntityTarget,
(subject) =>
subject.metadata.targetName === sortedEntityTarget ||
subject.metadata.parentEntityMetadata?.targetName,
)
sortedSubjects.push(...entityTargetSubjects)
this.removeAlreadySorted(entityTargetSubjects)
Expand Down
9 changes: 9 additions & 0 deletions test/github-issues/9241/entity/Employee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Column, ChildEntity } from "../../../../src"

import { User } from "./User"

@ChildEntity()
export class Employee extends User {
@Column()
salary: number
}
22 changes: 22 additions & 0 deletions test/github-issues/9241/entity/Photo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
OneToMany,
Entity,
PrimaryGeneratedColumn,
Column,
} from "../../../../src"

import { UserPhoto } from "./UserPhoto"

@Entity()
export class Photo {
@PrimaryGeneratedColumn()
id: number

@Column()
name: string

@OneToMany(() => UserPhoto, (userPhoto) => userPhoto.photo, {
cascade: true,
})
userPhotos: UserPhoto[]
}
24 changes: 24 additions & 0 deletions test/github-issues/9241/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
Entity,
PrimaryGeneratedColumn,
OneToMany,
Column,
TableInheritance,
} from "../../../../src"

import { UserPhoto } from "./UserPhoto"

@Entity()
@TableInheritance({ column: { type: "varchar", name: "type" } })
export class User {
@PrimaryGeneratedColumn()
id: number

@Column()
name: string

@OneToMany(() => UserPhoto, (userPhoto) => userPhoto.user, {
cascade: true,
})
userPhotos: UserPhoto[]
}
22 changes: 22 additions & 0 deletions test/github-issues/9241/entity/UserPhoto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Column, PrimaryColumn, ManyToOne, Entity } from "../../../../src"

import { User } from "./User"
import { Photo } from "./Photo"

@Entity()
export class UserPhoto {
@Column()
isProfilePhoto: boolean

@ManyToOne(() => User, (user) => user.userPhotos, { nullable: false })
user: User

@PrimaryColumn()
userId: User["id"]

@ManyToOne(() => Photo, (photo) => photo.userPhotos, { nullable: false })
photo: Photo

@PrimaryColumn()
photoId: Photo["id"]
}
58 changes: 58 additions & 0 deletions test/github-issues/9241/issue-9241.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import "reflect-metadata"
import { expect } from "chai"
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource, DeepPartial } from "../../../src"

import { Employee } from "./entity/Employee"
import { Photo } from "./entity/Photo"

describe("github issues > #9241 Incorrect insert order when cascade inserting parent inherited relations", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["sqlite", "better-sqlite3"],
schemaCreate: true,
dropSchema: true,
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("should save entities properly", async () => {
for (const connection of connections) {
const photos: DeepPartial<Photo>[] = [
{ name: "Photo 1" },
{ name: "Photo 2" },
]

await connection.getRepository(Photo).save(photos)

const employee: DeepPartial<Employee> = {
name: "test name",
salary: 12345,
userPhotos: [
{
photo: photos[0],
isProfilePhoto: true,
},
{
photo: photos[1],
isProfilePhoto: false,
},
],
}

const employeeRepository = connection.getRepository(Employee)
const createdEmployee = employeeRepository.create(employee)

await expect(employeeRepository.save(createdEmployee)).to.eventually
.be.fulfilled
}
})
})

0 comments on commit 14dfadb

Please sign in to comment.