Skip to content

Commit

Permalink
fix: createQueryBuilder relation remove works only if using ID (#2632) (
Browse files Browse the repository at this point in the history
  • Loading branch information
iMobs authored and pleerock committed Sep 13, 2019
1 parent 81f4b43 commit 1d73a90
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/query-builder/RelationRemover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class RelationRemover {
}),
...junctionMetadata.inverseColumns.map((column, columnIndex) => {
const parameterName = "secondValue_" + firstColumnValIndex + "_" + secondColumnValIndex + "_" + columnIndex;
parameters[parameterName] = firstColumnVal instanceof Object ? column.referencedColumn!.getEntityValue(secondColumnVal) : secondColumnVal;
parameters[parameterName] = secondColumnVal instanceof Object ? column.referencedColumn!.getEntityValue(secondColumnVal) : secondColumnVal;
return `${column.databaseName} = :${parameterName}`;
})
].join(" AND ");
Expand All @@ -108,4 +108,4 @@ export class RelationRemover {
}
}

}
}
18 changes: 18 additions & 0 deletions test/github-issues/2632/entity/Category.ts
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[];

}
20 changes: 20 additions & 0 deletions test/github-issues/2632/entity/Post.ts
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[];

}
75 changes: 75 additions & 0 deletions test/github-issues/2632/issue-2632.ts
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([]);

})));

});

0 comments on commit 1d73a90

Please sign in to comment.