Skip to content

docs(postgrest): document that order() defaults to descending - #1637

Merged
spydon merged 1 commit into
supabase:mainfrom
giovanirasia:patch-1
Aug 4, 2026
Merged

spydon merged 1 commit into
supabase:mainfrom
giovanirasia:patch-1

Conversation

@giovanirasia

@giovanirasia giovanirasia commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Documentation.

What is the current behavior?

order() declares bool ascending = false, so a call that omits the parameter
returns rows in descending order. The doc comment never states this. It only
says:

When [ascending] is true, the result will be in ascending order.

Both examples in the doc comment then pass ascending: false explicitly, which
suggests the opposite — that the parameter has to be passed to get descending
order, and that omitting it yields ascending.

This is easy to get wrong, because the default is inverted relative to two things
a developer is likely to expect:

  • SQL's ORDER BY, which defaults to ASC
  • postgrest-js, where order() destructures { ascending = true }

The failure is silent: the query succeeds and returns data, just in the wrong
order. In my case it survived unit tests and only surfaced when a historical
backfill read a series inverted.

There is also a small rendering bug in the same doc comment — the first code
fence is closed with four backticks instead of three, which breaks the block in
generated dartdoc.

What is the new behavior?

  • The doc comment states the defaults for ascending and nullsFirst
    explicitly, and notes the divergence from SQL and postgrest-js.
  • The first example now omits ascending, showing the actual default; a second
    example shows how to request ascending order.
  • The stray fourth backtick is fixed.

Doc comment only — no behavioural change. Changing the default itself would be
breaking and is deliberately not proposed here.

Additional context

Happy to open a follow-up for the equivalent JSDoc in postgrest-js, which
likewise does not state its default. Let me know if that would be useful.

Summary by CodeRabbit

  • Documentation
    • Clarified that descending order and nulls-last behavior are the defaults for ordering.
    • Added examples demonstrating how to explicitly request ascending order.

@giovanirasia
giovanirasia requested a review from a team as a code owner August 1, 2026 01:19
@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: a6a832dc-34a7-4677-8fab-b136b9a86c9a

📥 Commits

Reviewing files that changed from the base of the PR and between 6979093 and 5f64d35.

📒 Files selected for processing (1)
  • packages/postgrest/lib/src/postgrest_transform_builder.dart

📝 Walkthrough

Walkthrough

The order documentation now states that descending order and nulls-last behavior are defaults. It also explains differences from SQL and postgrest-js and adds examples for default descending and explicit ascending ordering.

Changes

Order documentation

Layer / File(s) Summary
Document order defaults
packages/postgrest/lib/src/postgrest_transform_builder.dart
The order documentation describes default descending and nulls-last behavior, differences from SQL and postgrest-js, and ascending-order examples.

Estimated code review effort: 1 (Trivial) | ~2 minutes

Suggested reviewers: spydon

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the documentation change for order()'s descending default.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@spydon spydon 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.

Thanks, we'll change the default of this in v3 (the default was changed in the js SDK a few years ago)

@spydon
spydon merged commit dc93d76 into supabase:main Aug 4, 2026
40 of 41 checks passed
spydon added a commit that referenced this pull request Aug 6, 2026
Closes #1638

## What kind of change does this PR introduce?

Breaking change.

## What is the current behavior?

`order()` defaults `ascending` to `false` in two places, so omitting the
parameter sorts descending:

- `PostgrestTransformBuilder.order()`
- `SupabaseStreamBuilder.order()`

`.order('created_at')` therefore returns newest rows first, which
disagrees with SQL's `ORDER BY` (defaults to `ASC`) and with
`postgrest-js` (`{ ascending = true }`). The failure is silent: the
query succeeds, the row count and types are right, only the order is
wrong.

The default was never chosen for Dart. `postgrest-dart` `7a8f8b7`
(2020-09-10) ported the first `order()` from the pre-TypeScript
`postgrest-js`, which then had `order(columnName, ascending = false,
nullsFirst = false)`. `postgrest-js` `2960d09` moved to `{ ascending =
true }` ten days later and Dart never followed. #1637 documented the
current behavior in the meantime.

Two examples in this repository were already wrong because of it, which
is the clearest evidence that the default surprises people:

- `examples/realtime_room/lib/room_repository.dart` says "SELECT the
existing messages once, oldest first" and returned them newest first.
- `examples/database_crud/lib/tasks_repository.dart` says "ordered
alphabetically by name" and returned projects in reverse alphabetical
order.

Both are now correct without touching their query code.

## What is the new behavior?

- `PostgrestTransformBuilder.order()`: `bool ascending = true`.
- `SupabaseStreamBuilder.order()`: `bool ascending = true`, so
`.stream()` and `.select()` agree on what an unqualified `.order()`
means.
- Doc comments on both state the default and drop the note about
diverging from SQL and `postgrest-js`, since there is no longer a
divergence.

`nullsFirst` is unchanged at `false`. That already matched
`postgrest-js` and PostgREST.

## Migration

`MIGRATION.md` gets an `order() now sorts ascending by default` section
under v2 to v3, covering both call sites, why the analyzer will not
catch it, and how to keep the old behavior:

```dart
await supabase.from('messages').select().order('created_at', ascending: false);
```

The section also points out that `ascending: false` already means
descending on v2, so consumers can add it before upgrading and leave
this change out of the upgrade itself.

## Tests

- New `packages/postgrest/test/order_default_test.dart` asserts the
generated `order` query parameter without a backend: the default,
explicit `ascending: false`, `nullsFirst: true`, a referenced table, and
that chained `order()` calls keep their own direction.
- `transforms_test.dart` gains `order defaults to ascending` and `order
descending` against the local stack.
- `mock_test.dart` gains `order defaults to ascending` for the stream
builder, and the mock server now answers `order=id.asc.nullslast`.
- Tests that were exercising the old implicit default (multi-column
order, order with filters, referenced-table order, embedded order,
stream order with limit) now pass `ascending: false` explicitly, so
their coverage of descending order is preserved rather than silently
reinterpreted.

`melos analyze` and `melos format` are clean. `packages/postgrest` (196
tests) and `packages/supabase` (136 tests) pass in full against a local
Supabase stack.

## Additional context

Part of the v3 umbrella (#1278).


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* Database queries and real-time streams now default to ascending order.
  * Descending order remains available with `ascending: false`.
* Ordering continues to support null placement, referenced data,
multiple columns, and filters.

* **Documentation**
* Updated ordering guidance, examples, and migration notes to reflect
the new default.

* **Tests**
* Expanded coverage for default, ascending, descending, nested,
referenced-table, filtered, and stream ordering.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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.

2 participants