Skip to content

Commit 9460296

Browse files
authored
fix: convert the join table ID to the referenceColumn ID type (#9887)
* fix: Convert the join table ID to the referenceColumn ID type * test: add auto-increment-id-as-string test * style: format auto-increment-id-as-string test
1 parent 938f94b commit 9460296

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ export class RawSqlResultsToEntityTransformer {
497497
column.referencedColumn!.databaseName,
498498
)
499499
],
500-
column,
500+
column.referencedColumn!,
501501
)
502502
}
503503
})
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import "reflect-metadata"
2+
import {
3+
closeTestingConnections,
4+
createTestingConnections,
5+
reloadTestingDatabases,
6+
} from "../../utils/test-utils"
7+
import { DataSource } from "../../../src/data-source/DataSource"
8+
import { User } from "./entity/User"
9+
import { Role } from "./entity/Role"
10+
11+
describe("other issues > auto-increment id as string", () => {
12+
let connections: DataSource[]
13+
before(
14+
async () =>
15+
(connections = await createTestingConnections({
16+
entities: [__dirname + "/entity/*{.js,.ts}"],
17+
})),
18+
)
19+
beforeEach(() => reloadTestingDatabases(connections))
20+
after(() => closeTestingConnections(connections))
21+
22+
it("should relationIds exist", () =>
23+
Promise.all(
24+
connections.map(async function (connection) {
25+
const role1 = new Role()
26+
role1.roleName = "#role 1"
27+
const role2 = new Role()
28+
role2.roleName = "#role 2"
29+
30+
const user = new User()
31+
user.userName = "#user 1"
32+
user.roles = [
33+
await connection.manager.save(role1),
34+
await connection.manager.save(role2),
35+
]
36+
37+
const user2 = await connection.manager.save(user)
38+
39+
const user3 = await connection.manager.findOne(User, {
40+
where: {
41+
userId: user2.userId,
42+
},
43+
loadRelationIds: true,
44+
})
45+
user3!.roles.length.should.be.equal(2)
46+
}),
47+
))
48+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Column } from "../../../../src/decorator/columns/Column"
2+
import { Entity } from "../../../../src/decorator/entity/Entity"
3+
4+
@Entity()
5+
export class Role {
6+
@Column({
7+
name: "role_id",
8+
primary: true,
9+
type: "int",
10+
generated: "increment",
11+
transformer: {
12+
to(value: object) {
13+
return value?.toString()
14+
},
15+
from(value: object) {
16+
return value?.toString()
17+
},
18+
},
19+
})
20+
roleId: string
21+
22+
@Column({ name: "role_name" })
23+
roleName: string
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { JoinTable, ManyToMany } from "../../../../src"
2+
import { Column } from "../../../../src/decorator/columns/Column"
3+
import { Entity } from "../../../../src/decorator/entity/Entity"
4+
import { Role } from "./Role"
5+
6+
@Entity()
7+
export class User {
8+
@Column({
9+
name: "user_id",
10+
primary: true,
11+
type: "int",
12+
generated: "increment",
13+
transformer: {
14+
to(value: object) {
15+
return value?.toString()
16+
},
17+
from(value: object) {
18+
return value?.toString()
19+
},
20+
},
21+
})
22+
userId: string
23+
24+
@Column({ name: "user_name" })
25+
userName: string
26+
27+
@ManyToMany((type) => Role)
28+
@JoinTable({
29+
name: "user_role",
30+
joinColumn: {
31+
name: "user_id",
32+
referencedColumnName: "userId",
33+
},
34+
inverseJoinColumn: {
35+
name: "role_id",
36+
referencedColumnName: "roleId",
37+
},
38+
})
39+
roles: Role[]
40+
}

0 commit comments

Comments
 (0)