Skip to content

Commit

Permalink
revert: regression in ordering by the relation property (#8346) (#8352)
Browse files Browse the repository at this point in the history
* Revert "fix: ordering by joined columns for PostgreSQL (#3736) (#8118)"

This reverts commit 1649882.

* test: add a test for leftJoinAndSelect with ordering by a relation property and a limit (#8346)
  • Loading branch information
antonku committed Nov 11, 2021
1 parent a92120a commit 0334d10
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 93 deletions.
25 changes: 4 additions & 21 deletions src/query-builder/SelectQueryBuilder.ts
Expand Up @@ -1973,7 +1973,7 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements

// we are skipping order by here because its not working in subqueries anyway
// to make order by working we need to apply it on a distinct query
const [selects, orderBys, subquerySelect] = this.createOrderByCombinedWithSelectExpression("distinctAlias");
const [selects, orderBys] = this.createOrderByCombinedWithSelectExpression("distinctAlias");
const metadata = this.expressionMap.mainAlias.metadata;
const mainAliasName = this.expressionMap.mainAlias.name;

Expand All @@ -1995,7 +1995,7 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements
rawResults = await new SelectQueryBuilder(this.connection, queryRunner)
.select(`DISTINCT ${querySelects.join(", ")}`)
.addSelect(selects)
.from(`(${this.clone().orderBy().addSelect(subquerySelect).getQuery()})`, "distinctAlias")
.from(`(${this.clone().orderBy().getQuery()})`, "distinctAlias")
.offset(this.expressionMap.skip)
.limit(this.expressionMap.take)
.orderBy(orderBys)
Expand Down Expand Up @@ -2062,7 +2062,7 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements
};
}

protected createOrderByCombinedWithSelectExpression(parentAlias: string): [ string, OrderByCondition, string] {
protected createOrderByCombinedWithSelectExpression(parentAlias: string): [ string, OrderByCondition] {

// if table has a default order then apply it
const orderBys = this.expressionMap.allOrderBys;
Expand Down Expand Up @@ -2102,24 +2102,7 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements
}
});

const subquerySelectString = Object.keys(orderBys)
.filter(orderCriteria => orderCriteria.includes("."))
.map(orderCriteria => {
const criteriaParts = orderCriteria.split(".");
const aliasName = criteriaParts[0];
const propertyPath = criteriaParts.slice(1).join(".");
const alias = this.expressionMap.findAliasByName(aliasName);
if (alias.type !== "join") {
return "";
}
const column = alias.metadata.findColumnWithPropertyPath(propertyPath);
const property = this.escape(alias.name) + "." + this.escape(column!.databaseName);
const propertyAlias = this.escape(DriverUtils.buildAlias(this.connection.driver, aliasName, column!.databaseName));
return [property, "AS", propertyAlias].join(" ");
})
.join(", ");

return [selectString, orderByObject, subquerySelectString];
return [selectString, orderByObject];
}

/**
Expand Down
16 changes: 0 additions & 16 deletions test/github-issues/3736/entity/Photo.ts

This file was deleted.

16 changes: 0 additions & 16 deletions test/github-issues/3736/entity/User.ts

This file was deleted.

40 changes: 0 additions & 40 deletions test/github-issues/3736/issue-3736.ts

This file was deleted.

21 changes: 21 additions & 0 deletions test/github-issues/8346/entity/Customer.ts
@@ -0,0 +1,21 @@
import {
BaseEntity,
Column,
Entity,
OneToMany,
PrimaryGeneratedColumn,
} from "../../../../src";

import { CustomerContact } from './CustomerContact';

@Entity('Customer')
export class Customer extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@OneToMany(() => CustomerContact, (contact) => contact.customer)
contacts: CustomerContact[];
}
25 changes: 25 additions & 0 deletions test/github-issues/8346/entity/CustomerContact.ts
@@ -0,0 +1,25 @@
import {
Column,
Entity,
Index,
ManyToOne,
PrimaryGeneratedColumn,
} from "../../../../src";

import { Customer } from './Customer';

@Entity('CustomerContact')
@Index(['firstName', 'lastName'])
export class CustomerContact {
@PrimaryGeneratedColumn()
id: number;

@ManyToOne(() => Customer, (customer) => customer.contacts)
customer: Customer;

@Column({ default: '' })
firstName: string;

@Column({ default: '' })
lastName: string;
}
42 changes: 42 additions & 0 deletions test/github-issues/8346/issue-8346.ts
@@ -0,0 +1,42 @@
import "reflect-metadata";
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { Connection } from "../../../src/connection/Connection";
import { expect } from "chai";
import { Customer } from "./entity/Customer";
import { CustomerContact } from "./entity/CustomerContact";


describe("github issues > #8346 MySQL: Regression when using take, orderBy, and getMany on a joined relation", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
logging: true
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should return customers ordered by contacts", () => Promise.all(connections.map(async connection => {
const [user1, user2] = await connection.getRepository(Customer).save([
{ name: "userA" },
{ name: "userB" },
]);
await connection.getRepository(CustomerContact).save([
{ firstName: "firstName1", lastName: "lastName1", customer: user1 },
{ firstName: "firstName2", lastName: "lastName2", customer: user2 },
]);

const customerRepository = connection.getRepository(Customer);

const results = await customerRepository
.createQueryBuilder('customer')
.leftJoinAndSelect('customer.contacts', 'contacts')
.take(10)
.orderBy('contacts.firstName', 'DESC')
.getMany();

expect(results[0].id).to.equal(user2.id);
expect(results[1].id).to.equal(user1.id);
})));
})

0 comments on commit 0334d10

Please sign in to comment.