-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: createQueryBuilder relation remove works only if using ID (#2632) (
- Loading branch information
Showing
4 changed files
with
115 additions
and
2 deletions.
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
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,18 @@ | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../src/decorator/columns/Column"; | ||
import {Post} from "./Post"; | ||
import {ManyToMany} from "../../../../src/decorator/relations/ManyToMany"; | ||
|
||
@Entity() | ||
export class Category { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@ManyToMany(type => Post, post => post.categories) | ||
posts: Post[]; | ||
|
||
} |
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,20 @@ | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../src/decorator/columns/Column"; | ||
import {Category} from "./Category"; | ||
import {ManyToMany} from "../../../../src/decorator/relations/ManyToMany"; | ||
import {JoinTable} from "../../../../src/decorator/relations/JoinTable"; | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@ManyToMany(type => Category, category => category.posts) | ||
@JoinTable() | ||
categories: Category[]; | ||
|
||
} |
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,75 @@ | ||
import "reflect-metadata"; | ||
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; | ||
import {Connection} from "../../../src/connection/Connection"; | ||
import {Post} from "./entity/Post"; | ||
import {Category} from "./entity/Category"; | ||
import {expect} from "chai"; | ||
|
||
describe("github issues > #2632 createQueryBuilder relation remove works only if using ID", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should add and remove relations of an entity if given a mix of ids and objects", () => Promise.all(connections.map(async connection => { | ||
|
||
const post1 = new Post(); | ||
post1.title = "post #1"; | ||
await connection.manager.save(post1); | ||
|
||
const post2 = new Post(); | ||
post2.title = "post #2"; | ||
await connection.manager.save(post2); | ||
|
||
const category1 = new Category(); | ||
category1.title = "category #1"; | ||
await connection.manager.save(category1); | ||
|
||
const category2 = new Category(); | ||
category2.title = "category #2"; | ||
await connection.manager.save(category2); | ||
|
||
await connection | ||
.createQueryBuilder() | ||
.relation(Post, "categories") | ||
.of(post1) | ||
.add(1); | ||
|
||
let loadedPost1 = await connection.manager.findOne(Post, 1, { relations: ["categories"] }); | ||
expect(loadedPost1!.categories).to.deep.include({ id: 1, title: "category #1" }); | ||
|
||
await connection | ||
.createQueryBuilder() | ||
.relation(Post, "categories") | ||
.of(post1) | ||
.remove(1); | ||
|
||
loadedPost1 = await connection.manager.findOne(Post, 1, { relations: ["categories"] }); | ||
expect(loadedPost1!.categories).to.be.eql([]); | ||
|
||
await connection | ||
.createQueryBuilder() | ||
.relation(Post, "categories") | ||
.of(2) | ||
.add(category2); | ||
|
||
let loadedPost2 = await connection.manager.findOne(Post, 2, { relations: ["categories"] }); | ||
expect(loadedPost2!.categories).to.deep.include({ id: 2, title: "category #2" }); | ||
|
||
await connection | ||
.createQueryBuilder() | ||
.relation(Post, "categories") | ||
.of(2) | ||
.remove(category2); | ||
|
||
loadedPost1 = await connection.manager.findOne(Post, 2, { relations: ["categories"] }); | ||
expect(loadedPost1!.categories).to.be.eql([]); | ||
|
||
}))); | ||
|
||
}); |