[PR #22026] fix: correct column order in workflow UNION query to fix sorting #21997

Open
opened 2025-11-20 06:22:16 -06:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/n8n-io/n8n/pull/22026
Author: @rishiraj-58
Created: 11/19/2025
Status: 🔄 Open

Base: masterHead: fix/workflow-sorting-column-order


📝 Commits (1)

  • 8c05171 fix: correct column order in workflow UNION query to fix sorting

📊 Changes

1 file changed (+1 additions, -1 deletions)

View changed files

📝 packages/@n8n/db/src/repositories/workflow.repository.ts (+1 -1)

📄 Description

Summary

This PR fixes the reversed workflow sorting behavior where "Sort by last updated" was sorting by creation date and "Sort by last created" was sorting by update date.

Root Cause

The commonFields object in buildBaseUnionQuery() method had columns ordered as { createdAt, updatedAt, ... }, but the actual entity columns (defined by the WithTimestamps base class) are ordered as { updatedAt, createdAt, ... }. This mismatch caused the SQL UNION query to swap data between these two columns when fetching workflows and folders together.

Changes

  • Reordered commonFields in workflow.repository.ts line 151-156 to match the actual entity column order
  • Changed from { createdAt: true, updatedAt: true, ... } to { updatedAt: true, createdAt: true, ... }

Testing Results

Before fix:

  • Clicking "Sort by last updated" → workflows sorted by creation date
  • Clicking "Sort by last created" → workflows sorted by update date

After fix:

  • Clicking "Sort by last updated" → workflows sorted by update date
  • Clicking "Sort by last created" → workflows sorted by creation date

Test Steps:

  1. Navigate to workflows page
  2. Create or update several workflows at different times
  3. Test "Sort by last updated" - should show most recently updated first
  4. Test "Sort by last created" - should show most recently created first
  5. Verify "Last updated" timestamps match the sort order

Notes

Closes #22016

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

Note

Reorders commonFields in workflow.repository.ts so updatedAt precedes createdAt, aligning UNION select column order to map timestamps correctly and fix sorting by updated/created.

Written by Cursor Bugbot for commit 8c05171a11. This will update automatically on new commits. Configure here.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/n8n-io/n8n/pull/22026 **Author:** [@rishiraj-58](https://github.com/rishiraj-58) **Created:** 11/19/2025 **Status:** 🔄 Open **Base:** `master` ← **Head:** `fix/workflow-sorting-column-order` --- ### 📝 Commits (1) - [`8c05171`](https://github.com/n8n-io/n8n/commit/8c05171a11c8da12a0b416d86765a3abfd76741f) fix: correct column order in workflow UNION query to fix sorting ### 📊 Changes **1 file changed** (+1 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/@n8n/db/src/repositories/workflow.repository.ts` (+1 -1) </details> ### 📄 Description ## Summary This PR fixes the reversed workflow sorting behavior where "Sort by last updated" was sorting by creation date and "Sort by last created" was sorting by update date. ### Root Cause The `commonFields` object in `buildBaseUnionQuery()` method had columns ordered as `{ createdAt, updatedAt, ... }`, but the actual entity columns (defined by the `WithTimestamps` base class) are ordered as `{ updatedAt, createdAt, ... }`. This mismatch caused the SQL UNION query to swap data between these two columns when fetching workflows and folders together. ### Changes - Reordered `commonFields` in `workflow.repository.ts` line 151-156 to match the actual entity column order - Changed from `{ createdAt: true, updatedAt: true, ... }` to `{ updatedAt: true, createdAt: true, ... }` ### Testing Results **Before fix:** - Clicking "Sort by last updated" → workflows sorted by creation date ❌ - Clicking "Sort by last created" → workflows sorted by update date ❌ **After fix:** - Clicking "Sort by last updated" → workflows sorted by update date ✅ - Clicking "Sort by last created" → workflows sorted by creation date ✅ **Test Steps:** 1. Navigate to workflows page 2. Create or update several workflows at different times 3. Test "Sort by last updated" - should show most recently updated first 4. Test "Sort by last created" - should show most recently created first 5. Verify "Last updated" timestamps match the sort order ### Notes - This fix aligns with the existing comment on lines 173-178 about column ordering being "load-bearing" - Related to the tech debt issue: https://linear.app/n8n/issue/ADO-4376/tech-debt-investigate-and-fix-root-cause-of-incorrect-sql-column ## Related Linear tickets, Github issues, and Community forum posts Closes #22016 ## Review / Merge checklist - [x] PR title and summary are descriptive. ([conventions](../blob/master/.github/pull_request_title_conventions.md)) - [ ] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up ticket created. <!-- No docs update needed - this is a bug fix --> - [ ] Tests included. <!-- Manual testing performed. Automated tests would require significant DB mocking setup for UNION queries --> - [ ] PR Labeled with `release/backport` (if the PR is an urgent fix that needs to be backported) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Reorders `commonFields` in `workflow.repository.ts` so `updatedAt` precedes `createdAt`, aligning UNION select column order to map timestamps correctly and fix sorting by updated/created. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 8c05171a11c8da12a0b416d86765a3abfd76741f. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the
pull-request
label 2025-11-20 06:22:16 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/n8n#21997
No description provided.