Skip to content

fix(postgrest): add typed column inference for order() with referencedTable#2445

Open
7vignesh wants to merge 1 commit into
supabase:masterfrom
7vignesh:fix/order-referenced-table-types
Open

fix(postgrest): add typed column inference for order() with referencedTable#2445
7vignesh wants to merge 1 commit into
supabase:masterfrom
7vignesh:fix/order-referenced-table-types

Conversation

@7vignesh

@7vignesh 7vignesh commented Jun 11, 2026

Copy link
Copy Markdown

Description

What changed?

Added new typed overloads to the order() method in PostgrestTransformBuilder that resolve the referenced table's column names from the Schema type when referencedTable (or deprecated foreignTable) is a known table/view.

Overload resolution now works in 3 tiers:

  1. No referencedTable → autocomplete shows columns from the parent table (unchanged)
  2. referencedTable is a known table/view → autocomplete shows columns from that table's Row, invalid columns produce a compile-time error
  3. referencedTable is an unknown string → falls through to column: string (unchanged, for dynamic/alias use cases)

The catch-all overload uses a conditional never type to prevent it from matching when the referenced table is known, forcing TypeScript to use the typed overload instead.

Why was this change needed?

When users passed referencedTable: 'messages' to .order(), TypeScript offered no autocomplete for the referenced table's columns and silently accepted invalid column names. This caused runtime errors that could have been caught at compile time.

This is the TypeScript typing portion of the issue — runtime behavior was already correct (clarified by maintainers in the issue thread).

Closes #971

📸 Screenshots/Examples

After: type error on invalid column for referenced table

image

After: valid column passes without error

image

Breaking changes

  • This PR contains no breaking changes

The catch-all string overload still exists for dynamic table names or unknown aliases, so existing code that passes arbitrary strings continues to compile.

Checklist

  • I have read the Contributing Guidelines
  • My PR title follows the conventional commit format: fix(postgrest): add typed column inference for order() with referencedTable
  • I have run pnpm nx format to ensure consistent code formatting
  • I have added tests for new functionality (type tests in test/index.test-d.ts)
  • I have updated documentation (if applicable)

Additional notes

  • This supersedes the stalled PR fix(postgrest): correct order() overload typing for referenced tables #2025 which attempted to fix this by deleting overloads (which would have broken base-case autocomplete, as noted by @avallete in review).
  • The fix imports TablesAndViews from the existing select-query-parser types no new type utilities were introduced.
  • Zero runtime changes. The implementation body of order() is untouched.
  • All 28 tstyche type test files pass. Build passes for both postgrest-js and downstream supabase-js.
  • Integration tests require Docker (PostgREST + PostgreSQL) which CI will handle. Since no runtime code changed, they will pass identically.

…dTable

When referencedTable or foreignTable is a known table/view,

the column param is now constrained to that table's Row.

Provides autocomplete and compile-time error checking.

Closes supabase#971
@7vignesh 7vignesh requested review from a team as code owners June 11, 2026 20:34
Copilot AI review requested due to automatic review settings June 11, 2026 20:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates Postgrest-js TypeScript typings so .order() can infer valid column names when ordering via a referencedTable (and the deprecated foreignTable) based on the schema.

Changes:

  • Add TablesAndViews-based overloads for .order() to type column names for referencedTable.
  • Add matching typed overloads for deprecated foreignTable.
  • Extend d.ts tests to cover typed ordering on referenced tables and expected type errors.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
packages/core/postgrest-js/src/PostgrestTransformBuilder.ts Adds schema-aware .order() overloads for referencedTable/foreignTable to type referenced-table columns.
packages/core/postgrest-js/test/index.test-d.ts Adds type-level tests verifying typed referenced-table ordering and invalid-column failures.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +148 to +156
order<ReferencedTable extends string>(
column: string,
options?: { ascending?: boolean; nullsFirst?: boolean; referencedTable?: string }
options?: {
ascending?: boolean
nullsFirst?: boolean
referencedTable?: ReferencedTable extends keyof TablesAndViews<Schema>
? never
: ReferencedTable
}
Comment on lines +178 to +184
order<ReferencedTable extends string>(
column: string,
options?: { ascending?: boolean; nullsFirst?: boolean; foreignTable?: string }
options?: {
ascending?: boolean
nullsFirst?: boolean
foreignTable?: ReferencedTable extends keyof TablesAndViews<Schema> ? never : ReferencedTable
}
Comment on lines +141 to +147
order<
ReferencedTable extends string & keyof TablesAndViews<Schema>,
ColumnName extends string & keyof TablesAndViews<Schema>[ReferencedTable]['Row'],
>(
column: ColumnName,
options: { ascending?: boolean; nullsFirst?: boolean; referencedTable: ReferencedTable }
): this
Comment on lines +168 to +174
order<
ReferencedTable extends string & keyof TablesAndViews<Schema>,
ColumnName extends string & keyof TablesAndViews<Schema>[ReferencedTable]['Row'],
>(
column: ColumnName,
options: { ascending?: boolean; nullsFirst?: boolean; foreignTable: ReferencedTable }
): this
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 on a foreign table

2 participants