Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ordering by joined columns for PostgreSQL (#3736) #8118

Merged
merged 1 commit into from Nov 5, 2021

Conversation

antonku
Copy link
Contributor

@antonku antonku commented Aug 25, 2021

Description of change

Fixes #3736

This PR adds the missing column to the select clause inside the sub query when ordering is done by a property of a related entity.

Example:

const queryBuilder: SelectQueryBuilder<Photo> = await connection.getRepository(Photo).createQueryBuilder("photo");
await queryBuilder
    .select()
    .leftJoin("photo.user", "user")
    .take(5)
    .orderBy("user.name")
    .getManyAndCount();

Generates the following valid SQL query:

SELECT
	DISTINCT "distinctAlias"."photo_id" AS "ids_photo_id",
	"distinctAlias"."user_name"
FROM
	(
		SELECT "photo"."id" AS "photo_id",
		"photo"."url" AS "photo_url",
		"photo"."userId" AS "photo_userId",
+		"user"."name" AS "user_name" 
	FROM
		"photo" "photo"
	LEFT JOIN "user" "user" ON
		"user"."id" = "photo"."userId") "distinctAlias"
ORDER BY
	"distinctAlias"."user_name" ASC,
	"photo_id" ASC
LIMIT 5

Without the fix "user"."name" AS "user_name" line is missing and query fails with error:

error: column distinctAlias.user_name does not exist

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run lint passes with this change
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

@antonku
Copy link
Contributor Author

antonku commented Oct 28, 2021

@pleerock Hi! Can you please review this PR?

@pleerock
Copy link
Member

pleerock commented Nov 5, 2021

Let's hope it won't lead to other issues. Thanks for contribution!

@pleerock pleerock merged commit 1649882 into typeorm:master Nov 5, 2021
@cjbara
Copy link

cjbara commented Nov 9, 2021

@pleerock and @antonku, FYI this produces an invalid SQL query if the orderBy is passed in from FindOptions. It can generate a SQL query with trailing commas in the select statement because subquerySelectString can contain empty string values. In this case, the join(", ") adds the trailing commas, producing an invalid SQL query. I just had this issue when I updated my TypeORM version

@antonku
Copy link
Contributor Author

antonku commented Nov 9, 2021

@cjbara Hi, thank you for the feedback. Can you please provide an example of FindOptions that produces the error?

antonku added a commit to antonku/typeorm that referenced this pull request Nov 10, 2021
pleerock pushed a commit that referenced this pull request Nov 11, 2021
* 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)
HeartPattern pushed a commit to HeartPattern/typeorm that referenced this pull request Nov 29, 2021
HeartPattern pushed a commit to HeartPattern/typeorm that referenced this pull request Nov 29, 2021
typeorm#8352)

* Revert "fix: ordering by joined columns for PostgreSQL (typeorm#3736) (typeorm#8118)"

This reverts commit 1649882.

* test: add a test for leftJoinAndSelect with ordering by a relation property and a limit (typeorm#8346)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Order by joined column broken in Postgres
4 participants