Skip to content

fix(datagrid): keep query result columns in SELECT order (#1565)#1570

Merged
datlechin merged 2 commits into
mainfrom
fix/1565-query-column-order
Jun 3, 2026
Merged

fix(datagrid): keep query result columns in SELECT order (#1565)#1570
datlechin merged 2 commits into
mainfrom
fix/1565-query-column-order

Conversation

@datlechin
Copy link
Copy Markdown
Member

Fixes #1565.

Problem

In a SQL/query tab, adding a column in the middle of the SELECT (for example select id, business_model then select id, state, business_model) showed the new column at the end of the grid instead of its query position. Same effect when a previously dragged column order from an earlier query was reapplied to a different result.

Root cause

For query tabs the per-tab layout (tab.columnLayout) captured a columnOrder whenever a column was resized or dragged, and nothing cleared it when the query re-ran with a different column set. On the next grid rebuild that stale order was reapplied and any column not in it was appended at the end. The per-table file persister was never involved; this was the in-memory per-tab layout leaking into a changed result.

Fix

Query results now always follow the order written in the SELECT:

  • savedColumnLayout no longer restores a saved column order for non-table tabs. It still keeps remembered column widths. Table-tab behavior is unchanged (per-table order still persists).
  • A column missing from saved widths now auto-fits instead of falling back to a hardcoded 100px, so a newly added column gets a sensible width.

This matches TablePlus, Postico, and Sequel Ace, where query results follow the SELECT and persistent column order is a table-browse feature.

Behavior note

A column drag in a query result is now transient: it holds for the current view and reverts to query order on the next page change, sort, refresh, or re-run. Persistent custom column order remains available on table tabs.

Tests

TableViewCoordinatorLayoutTests now covers that a stale saved order is dropped while widths survive, and that an empty layout returns nil.

Out of scope

Column order and width are keyed by column name, so a join returning two columns named id cannot be keyed reliably. This fix sidesteps it for query tabs. A position-stable column identifier would be a proper follow-up.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5a3baf65d8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

guard tabType == .table else {
guard !binding.columnWidths.isEmpty else { return nil }
var layout = binding
layout.columnOrder = nil
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep query column drags from snapping back immediately

When a user drags a column in a query result, tableViewColumnDidMove immediately calls persistColumnLayoutToStorage(), which writes the captured order back through the columnLayout binding; that state change causes updateNSView to call savedColumnLayout and reconcile the pool again. Because this line strips columnOrder for every non-table tab, the next SwiftUI update recomputes schema/SELECT order and moves the column back right after the drag, rather than keeping the drag for the current view as intended.

Useful? React with 👍 / 👎.

Signed-off-by: Ngô Quốc Đạt <datlechin@gmail.com>
@datlechin datlechin merged commit e582a8b into main Jun 3, 2026
4 checks passed
@datlechin datlechin deleted the fix/1565-query-column-order branch June 3, 2026 10:26
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.

New columns appear at end

1 participant