Skip to content

Commit

Permalink
fix: convert the join table ID to the referenceColumn ID type (#9887)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tqh177 committed May 9, 2023
1 parent 938f94b commit 9460296
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 1 deletion.
Expand Up @@ -497,7 +497,7 @@ export class RawSqlResultsToEntityTransformer {
column.referencedColumn!.databaseName,
)
],
column,
column.referencedColumn!,
)
}
})
Expand Down
@@ -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)
}),
))
})
24 changes: 24 additions & 0 deletions test/other-issues/auto-increment-id-as-string/entity/Role.ts
@@ -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
}
40 changes: 40 additions & 0 deletions test/other-issues/auto-increment-id-as-string/entity/User.ts
@@ -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[]
}

0 comments on commit 9460296

Please sign in to comment.