-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle inherited relations insert order (#9242)
Take inheritance into consideration when sorting insert commands Closes: #9241 Co-authored-by: Kevin KONRAD <ext.kevin.konrad@reseau.sncf.fr>
- Loading branch information
1 parent
e519910
commit 14dfadb
Showing
6 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) | ||
}) |