Skip to content

Commit a0d190d

Browse files
authored
test: verify synchronize true works with multiple relations (typeorm#11889)
1 parent 77b6ca9 commit a0d190d

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

test/functional/connection/connection.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import { Guest as GuestV1 } from "./entity/v1/Guest"
2424
import { Comment as CommentV2 } from "./entity/v2/Comment"
2525
import { Guest as GuestV2 } from "./entity/v2/Guest"
2626
import { View } from "./entity/View"
27+
import { Professor } from "./entity/Professor"
28+
import { Subject } from "./entity/Subject"
29+
import { SiteLocation } from "./entity/SiteLocation"
30+
import { Site } from "./entity/Site"
2731

2832
describe("Connection", () => {
2933
// const resourceDir = __dirname + "/../../../../../test/functional/connection/";
@@ -414,4 +418,81 @@ describe("Connection", () => {
414418
}),
415419
))
416420
})
421+
422+
// GitHub issue #7738
423+
describe("synchronize with multiple foreign keys to same table", () => {
424+
let connections: DataSource[]
425+
beforeEach(
426+
async () =>
427+
(connections = await createTestingConnections({
428+
enabledDrivers: ["postgres"],
429+
entities: [Professor, Subject, Site, SiteLocation],
430+
dropSchema: true,
431+
})),
432+
)
433+
afterEach(() => closeTestingConnections(connections))
434+
435+
it("should not fail with constraint already exists error when synchronizing multiple times", () =>
436+
Promise.all(
437+
connections.map(async (dataSource) => {
438+
// First synchronization
439+
await dataSource.synchronize()
440+
441+
// Second synchronization should not fail with constraint error
442+
await expect(dataSource.synchronize()).to.not.be.rejected
443+
444+
const professorRepo = dataSource.getRepository(Professor)
445+
const subjectRepo = dataSource.getRepository(Subject)
446+
const siteRepo = dataSource.getRepository(Site)
447+
const siteLocationRepo =
448+
dataSource.getRepository(SiteLocation)
449+
450+
// Create and save entities to test foreign key relationships
451+
const professor = professorRepo.create({
452+
name: "Dr. Smith",
453+
})
454+
await professorRepo.save(professor)
455+
456+
const assistant = professorRepo.create({
457+
name: "Dr. Jones",
458+
})
459+
await professorRepo.save(assistant)
460+
461+
const subject = subjectRepo.create({
462+
name: "Mathematics",
463+
professor: professor,
464+
assistant: assistant,
465+
})
466+
await subjectRepo.save(subject)
467+
468+
const loadedSubject = await subjectRepo.findOne({
469+
where: { id: subject.id },
470+
relations: ["professor", "assistant"],
471+
})
472+
473+
const site = siteRepo.create({ name: "Main Campus" })
474+
await siteRepo.save(site)
475+
476+
const siteLocation = siteLocationRepo.create({
477+
address: "123 Main St",
478+
site: site,
479+
})
480+
await siteLocationRepo.save(siteLocation)
481+
482+
const loadedSiteLocation = await siteLocationRepo.findOne({
483+
where: { id: siteLocation.id },
484+
relations: ["site"],
485+
})
486+
487+
expect(loadedSiteLocation).to.not.be.null
488+
expect(loadedSiteLocation!.site.name).to.equal(
489+
"Main Campus",
490+
)
491+
492+
expect(loadedSubject).to.not.be.null
493+
expect(loadedSubject!.professor.name).to.equal("Dr. Smith")
494+
expect(loadedSubject!.assistant.name).to.equal("Dr. Jones")
495+
}),
496+
))
497+
})
417498
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {
2+
PrimaryGeneratedColumn,
3+
Column,
4+
OneToMany,
5+
Entity,
6+
} from "../../../../src"
7+
import { Subject } from "./Subject"
8+
9+
@Entity()
10+
export class Professor {
11+
@PrimaryGeneratedColumn()
12+
id: number
13+
14+
@Column()
15+
name: string
16+
17+
@OneToMany(() => Subject, (subject) => subject.professor)
18+
subjects: Subject[]
19+
20+
@OneToMany(() => Subject, (subject) => subject.assistant)
21+
assistedSubjects: Subject[]
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Entity, Column, PrimaryGeneratedColumn } from "../../../../src"
2+
3+
@Entity()
4+
export class Site {
5+
@PrimaryGeneratedColumn()
6+
id: number
7+
8+
@Column()
9+
name: string
10+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {
2+
Entity,
3+
Column,
4+
PrimaryGeneratedColumn,
5+
OneToOne,
6+
JoinColumn,
7+
} from "../../../../src"
8+
import { Site } from "./Site"
9+
10+
@Entity()
11+
export class SiteLocation {
12+
@PrimaryGeneratedColumn()
13+
id: number
14+
15+
@Column()
16+
address: string
17+
18+
@OneToOne(() => Site)
19+
@JoinColumn()
20+
site: Site
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {
2+
Column,
3+
ManyToOne,
4+
PrimaryGeneratedColumn,
5+
Entity,
6+
} from "../../../../src"
7+
import { Professor } from "./Professor"
8+
9+
@Entity()
10+
export class Subject {
11+
@PrimaryGeneratedColumn()
12+
id: number
13+
14+
@Column()
15+
name: string
16+
17+
@ManyToOne(() => Professor, (professor) => professor.subjects)
18+
professor: Professor
19+
20+
@ManyToOne(() => Professor, (professor) => professor.assistedSubjects)
21+
assistant: Professor
22+
}

0 commit comments

Comments
 (0)