Skip to content

Commit

Permalink
fix: ensure distinct property is respected cloning query builder (#4843)
Browse files Browse the repository at this point in the history
* fix #4842: ensure select distinct property is respected when cloning query builder.

* Remove unused import statement.

* Node v12? added reflect-metadata import.

* Address lint issue.
  • Loading branch information
swim authored and pleerock committed Oct 18, 2019
1 parent fdab1b1 commit ea17094
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/query-builder/QueryExpressionMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ export class QueryExpressionMap {
const map = new QueryExpressionMap(this.connection);
map.queryType = this.queryType;
map.selects = this.selects.map(select => select);
map.selectDistinct = this.selectDistinct;
this.aliases.forEach(alias => map.aliases.push(new Alias(alias)));
map.mainAlias = this.mainAlias;
map.valuesSet = this.valuesSet;
Expand Down
12 changes: 12 additions & 0 deletions test/github-issues/4842/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Column, Entity, PrimaryGeneratedColumn} from "../../../../src/index";

@Entity()
export class Post {

@PrimaryGeneratedColumn()
id: number;

@Column()
title: string;

}
23 changes: 23 additions & 0 deletions test/github-issues/4842/issue-4842.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import "reflect-metadata";
import {expect} from "chai";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {Post} from "./entity/Post";

describe("github issues > #4842 QueryExpressionMap doesn't clone distinct property", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should contain correct distinct value after query builder is cloned", () => Promise.all(connections.map(async connection => {
const query = connection.manager.createQueryBuilder(Post, "post")
.distinct()
.disableEscaping();
const sqlWithDistinct = query.getSql();

expect(query.clone().getSql()).to.equal(sqlWithDistinct);
})));
});

0 comments on commit ea17094

Please sign in to comment.