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
This commit is contained in:
tqh177 2023-05-09 18:59:06 +08:00 committed by GitHub
parent 938f94bded
commit 9460296147
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 113 additions and 1 deletions

View File

@ -497,7 +497,7 @@ export class RawSqlResultsToEntityTransformer {
column.referencedColumn!.databaseName,
)
],
column,
column.referencedColumn!,
)
}
})

View File

@ -0,0 +1,48 @@
import "reflect-metadata"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/DataSource"
import { User } from "./entity/User"
import { Role } from "./entity/Role"
describe("other issues > auto-increment id as string", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))
it("should relationIds exist", () =>
Promise.all(
connections.map(async function (connection) {
const role1 = new Role()
role1.roleName = "#role 1"
const role2 = new Role()
role2.roleName = "#role 2"
const user = new User()
user.userName = "#user 1"
user.roles = [
await connection.manager.save(role1),
await connection.manager.save(role2),
]
const user2 = await connection.manager.save(user)
const user3 = await connection.manager.findOne(User, {
where: {
userId: user2.userId,
},
loadRelationIds: true,
})
user3!.roles.length.should.be.equal(2)
}),
))
})

View File

@ -0,0 +1,24 @@
import { Column } from "../../../../src/decorator/columns/Column"
import { Entity } from "../../../../src/decorator/entity/Entity"
@Entity()
export class Role {
@Column({
name: "role_id",
primary: true,
type: "int",
generated: "increment",
transformer: {
to(value: object) {
return value?.toString()
},
from(value: object) {
return value?.toString()
},
},
})
roleId: string
@Column({ name: "role_name" })
roleName: string
}

View File

@ -0,0 +1,40 @@
import { JoinTable, ManyToMany } from "../../../../src"
import { Column } from "../../../../src/decorator/columns/Column"
import { Entity } from "../../../../src/decorator/entity/Entity"
import { Role } from "./Role"
@Entity()
export class User {
@Column({
name: "user_id",
primary: true,
type: "int",
generated: "increment",
transformer: {
to(value: object) {
return value?.toString()
},
from(value: object) {
return value?.toString()
},
},
})
userId: string
@Column({ name: "user_name" })
userName: string
@ManyToMany((type) => Role)
@JoinTable({
name: "user_role",
joinColumn: {
name: "user_id",
referencedColumnName: "userId",
},
inverseJoinColumn: {
name: "role_id",
referencedColumnName: "roleId",
},
})
roles: Role[]
}