Skip to content

Commit e205139

Browse files
test: transformed the test: add City, Country, and Order entities with composite foreign key relations,
1 parent 69973ef commit e205139

File tree

8 files changed

+125
-134
lines changed

8 files changed

+125
-134
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Column, Entity, PrimaryColumn, Unique } from "../../../../../src"
2+
3+
@Entity()
4+
@Unique(["id", "countryId"])
5+
export class City {
6+
@PrimaryColumn()
7+
id: number
8+
9+
@Column()
10+
countryId: number
11+
12+
@Column()
13+
name: string
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Column, Entity, PrimaryColumn } from "../../../../../src"
2+
3+
@Entity()
4+
export class Country {
5+
@PrimaryColumn()
6+
id: number
7+
8+
@Column()
9+
name: string
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {
2+
Column,
3+
Entity,
4+
JoinColumn,
5+
ManyToOne,
6+
PrimaryGeneratedColumn,
7+
} from "../../../../../src"
8+
import { City } from "./City"
9+
import { Country } from "./Country"
10+
11+
@Entity()
12+
export class Order {
13+
@PrimaryGeneratedColumn()
14+
id: number
15+
16+
@Column()
17+
countryId: number
18+
19+
@ManyToOne(() => Country)
20+
country: Country
21+
22+
@Column()
23+
cityId: number
24+
25+
@ManyToOne(() => City)
26+
@JoinColumn([
27+
{ name: "cityId", referencedColumnName: "id" },
28+
{ name: "countryId", referencedColumnName: "countryId" },
29+
])
30+
city: City
31+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { expect } from "chai"
2+
3+
import { DataSource } from "../../../../src"
4+
5+
import {
6+
closeTestingConnections,
7+
createTestingConnections,
8+
reloadTestingDatabases,
9+
} from "../../../utils/test-utils"
10+
import { City } from "./entity/City"
11+
import { Country } from "./entity/Country"
12+
import { Order } from "./entity/Order"
13+
14+
describe("metadata builder > RelationJoinColumnBuilder", () => {
15+
let dataSources: DataSource[]
16+
17+
before(
18+
async () =>
19+
(dataSources = await createTestingConnections({
20+
entities: [__dirname + "/entity/*{.js,.ts}"],
21+
})),
22+
)
23+
beforeEach(() => reloadTestingDatabases(dataSources))
24+
after(() => closeTestingConnections(dataSources))
25+
26+
it("should not throw error when loading entities with composite FK with shared columns", async () =>
27+
Promise.all(
28+
dataSources.map(async (dataSource) => {
29+
await dataSource.getRepository(Country).save([
30+
{ id: 1, name: "Japan" },
31+
{ id: 2, name: "England" },
32+
])
33+
34+
await dataSource.getRepository(City).save([
35+
{ id: 1, countryId: 1, name: "Tokyo" },
36+
{ id: 2, countryId: 1, name: "Osaka" },
37+
{ id: 3, countryId: 2, name: "London" },
38+
{ id: 4, countryId: 2, name: "Manchester" },
39+
{ id: 5, countryId: 2, name: "Liverpool" },
40+
])
41+
42+
await dataSource.getRepository(Order).save([
43+
{ id: 1, countryId: 1, cityId: 1 },
44+
{ id: 2, countryId: 2, cityId: 4 },
45+
])
46+
47+
const Orders = await dataSource.getRepository(Order).find({
48+
relations: { city: true, country: true },
49+
order: { id: "asc" },
50+
})
51+
52+
expect(Orders).to.deep.members([
53+
{
54+
id: 1,
55+
countryId: 1,
56+
cityId: 1,
57+
city: { id: 1, countryId: 1, name: "Tokyo" },
58+
country: { id: 1, name: "Japan" },
59+
},
60+
{
61+
id: 2,
62+
countryId: 2,
63+
cityId: 4,
64+
city: { id: 4, countryId: 2, name: "Manchester" },
65+
country: { id: 2, name: "England" },
66+
},
67+
])
68+
}),
69+
))
70+
})

test/github-issues/10121/entity/city.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/github-issues/10121/entity/estate.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/github-issues/10121/entity/pref.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/github-issues/10121/issue-10121.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)