Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix entityManager.getId for custom join table (#8676)
getId currently returns undefined for an entity with composite primary key if primary key columns also foreign keys with lazy relations, for e.g., in a custom join table. This commit tries to fix that Closes: #7736 (maybe)
- Loading branch information
1 parent
3131c5c
commit 33b2bd7
Showing
5 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
test/other-issues/get-id-for-composite-primary-key/entity/Category.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class Category { | ||
@PrimaryGeneratedColumn("increment") | ||
id!: number; | ||
|
||
@Column() | ||
name!: string; | ||
} |
10 changes: 10 additions & 0 deletions
10
test/other-issues/get-id-for-composite-primary-key/entity/Post.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn("increment") | ||
id!: number; | ||
|
||
@Column() | ||
content!: string; | ||
} |
24 changes: 24 additions & 0 deletions
24
test/other-issues/get-id-for-composite-primary-key/entity/PostCategory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { | ||
CreateDateColumn, | ||
Entity, | ||
ManyToOne, | ||
PrimaryColumn, | ||
} from "../../../../src"; | ||
import { Category } from "./Category"; | ||
import { Post } from "./Post"; | ||
|
||
@Entity() | ||
export class PostCategory { | ||
@ManyToOne(() => Post) | ||
post!: Promise<Post>; | ||
@PrimaryColumn() | ||
postId!: Post["id"]; | ||
|
||
@ManyToOne(() => Category) | ||
category!: Promise<Category>; | ||
@PrimaryColumn() | ||
categoryId!: Category["id"]; | ||
|
||
@CreateDateColumn() | ||
added!: Date; | ||
} |
47 changes: 47 additions & 0 deletions
47
test/other-issues/get-id-for-composite-primary-key/get-id-for-composite-primary-key.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { expect } from "chai"; | ||
import "reflect-metadata"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils"; | ||
import { Category } from "./entity/Category"; | ||
import { Post } from "./entity/Post"; | ||
import { PostCategory } from "./entity/PostCategory"; | ||
|
||
describe("other issues > getId should not return undefined for composite primary keys with lazy relations", () => { | ||
let connections: Connection[]; | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
})) | ||
); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("getId should not return undefined", () => | ||
Promise.all( | ||
connections.map(async ({ manager }) => { | ||
const post = manager.create(Post, { | ||
content: "Sample Post", | ||
}); | ||
await manager.save(post); | ||
|
||
const category = manager.create(Category, { | ||
name: "javascript", | ||
}); | ||
await manager.save(category); | ||
|
||
const postCategory = manager.create(PostCategory, {}); | ||
postCategory.post = Promise.resolve(post); | ||
postCategory.category = Promise.resolve(category); | ||
await manager.save(postCategory); | ||
|
||
expect(manager.getId(post)).not.to.be.undefined; | ||
expect(manager.getId(category)).not.to.be.undefined; | ||
expect(manager.getId(postCategory)).not.to.be.undefined; | ||
}) | ||
)); | ||
}); |