fix(database): add sort columns to select to avoid DISTINCT subquery error#2564
Open
suvvvv wants to merge 2 commits into
Open
fix(database): add sort columns to select to avoid DISTINCT subquery error#2564suvvvv wants to merge 2 commits into
suvvvv wants to merge 2 commits into
Conversation
…error Sorting a query by a column that was not in the select crashed with `column "distinctAlias.<Model>_<col>" does not exist` on models that load relations (e.g. timeline models via @CanAccessIfCanReadOn). TypeORM paginates such queries with a DISTINCT subquery and references the sort columns in the outer ORDER BY; if a sort column is not selected, it is absent from the inner subquery and Postgres cannot resolve it. _findBy now adds every scalar sort column to the select (generalizing the previous createdAt-only handling) and strips the auto-added columns from the returned items so callers still get back only what they requested. Nested/relation sorts are skipped. Adds regression tests covering: the sort column being selected, the auto-added column being stripped, an explicitly selected sort column being preserved, and the default createdAt sort still working. Fixes OneUptime#2476 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes a 500 error when querying a model by a sort column that was not explicitly listed in
select, on models that load relations.GET /api/incident-state-timeline/get-list(oralert-state-timeline) withsort: {"startsAt": "ASC"}currently fails with:Root cause
These models load relations (via
@CanAccessIfCanReadOn), so TypeORM paginates with a DISTINCT subquery and references the sort columns in the outerORDER BY:startsAtis in the outerORDER BYbut not in the innerSELECT, so Postgres cannot resolve it.Fix
In
DatabaseService._findBy, ensure every scalar sort column is present in theselectmap before callingrepository.find(), and strip the auto-added columns from the returned items so callers only get back what they requested.createdAt-only handling — the defaultcreatedAtsort now flows through the same path.Tests
New
Common/Tests/Server/Services/DatabaseService.test.tswith 4 cases:selectpassed to the repository.createdAtsort is applied and stripped (no regression).The two bug-specific tests fail without the source change, confirming they are real regression guards.
Prettier-clean and type-checks pass.
Fixes #2476