diff --git a/src/query-builder/QueryExpressionMap.ts b/src/query-builder/QueryExpressionMap.ts index c65d337c78..2c86c82cbc 100644 --- a/src/query-builder/QueryExpressionMap.ts +++ b/src/query-builder/QueryExpressionMap.ts @@ -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; diff --git a/test/github-issues/4842/entity/Post.ts b/test/github-issues/4842/entity/Post.ts new file mode 100644 index 0000000000..411732ceb3 --- /dev/null +++ b/test/github-issues/4842/entity/Post.ts @@ -0,0 +1,12 @@ +import {Column, Entity, PrimaryGeneratedColumn} from "../../../../src/index"; + +@Entity() +export class Post { + + @PrimaryGeneratedColumn() + id: number; + + @Column() + title: string; + +} diff --git a/test/github-issues/4842/issue-4842.ts b/test/github-issues/4842/issue-4842.ts new file mode 100644 index 0000000000..457812630b --- /dev/null +++ b/test/github-issues/4842/issue-4842.ts @@ -0,0 +1,24 @@ +// import "reflect-metadata"; + +import {expect} from "chai"; +import {Post} from "./entity/Post"; +import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; +import {Connection} from "../../../src/connection/Connection"; + +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); + }))); +});