Skip to content

fix(postgrest): throw on multi-row result from maybeSingle when throw-on-error is enabled - #1180

Open
grdsdev wants to merge 2 commits into
mainfrom
fix/postgrest-maybesingle-multi-row
Open

fix(postgrest): throw on multi-row result from maybeSingle when throw-on-error is enabled#1180
grdsdev wants to merge 2 commits into
mainfrom
fix/postgrest-maybesingle-multi-row

Conversation

@grdsdev

@grdsdev grdsdev commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

What

maybeSingle() unconditionally swallowed PGRST116 ("no/too many rows"), so a query that unexpectedly matched more than one row silently returned nil instead of surfacing an error — hiding a real bug where the query should have been scoped to match at most one row.

  • Zero rows: still returns nil (unchanged)
  • More than one row: now throws a PostgrestError, matching the behavior of single()

PostgREST reports both cases with the same PGRST116 code; the row count is only distinguishable via the details message ("Results contain N rows, application/vnd.pgrst.object+json requires 1 row"), so maybeSingle() now inspects that to decide whether to swallow or rethrow.

Fixes #1170

Depends on #1159

This fix relies on PostgrestError.details actually decoding from the server response — which it doesn't today on main (see #1159: the wire key is "details", but the property/synthesized decoding key was detail, so it silently dropped).

The first commit on this branch (fix(helpers): decode PostgREST error details field) mirrors #1159's change so this PR is self-contained and testable independently. Once #1159 merges into main, rebasing this branch will produce a trivial no-op conflict on that one commit (identical change) — drop it and keep the second commit.

Verification

  • swift test --filter "PostgRESTTests|HelpersTests" — 229 tests pass
  • swift package diagnose-api-breaking-changes origin/main — no breaking changes in any module
  • ./scripts/format.sh — no-op

Acceptance criteria

  • Fix implemented matching the intended maybeSingle() behavior
  • Unit tests cover zero-row (unchanged), single-row (unchanged), and multi-row (new, throws)
  • Doc comments updated
  • No breaking changes to existing public API

grdsdev added 2 commits August 6, 2026 17:06
PostgREST sends the detail text under the "details" key, but PostgrestError
declares the property as "detail" with no CodingKeys, and the shared decoder
sets no key strategy. Every PostgREST error therefore lost its detail text.

Map detail to the "details" wire key so the value survives decoding, and
encoding round-trips to the same shape.

Mirrors #1159 (AndroidPoet), needed here so maybeSingle()'s multi-row fix in
the next commit can rely on error.details actually decoding. Rebase to drop
this commit once #1159 lands on main.
…-on-error is enabled

maybeSingle() swallowed PGRST116 unconditionally, so a query that matched
more than one row silently returned nil instead of surfacing the error --
hiding a real bug where the query should have been scoped to match at most
one row. Zero rows still returns nil; more than one row now throws, same as
single() already does.

PostgREST reports both cases with the same error code, distinguishable only
via the details message ("Results contain N rows, ..."), so maybeSingle now
inspects that to decide whether to swallow or rethrow.

Fixes #1170
@grdsdev
grdsdev requested a review from a team as a code owner August 6, 2026 20:08
@coderabbitai

coderabbitai Bot commented Aug 6, 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: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 34b7167c-6384-4367-aabe-76e2fe2ceb82

📥 Commits

Reviewing files that changed from the base of the PR and between 0826b28 and cac1794.

📒 Files selected for processing (7)
  • Sources/Helpers/SharedModels/PostgrestError.swift
  • Sources/PostgREST/PostgrestBuilder.swift
  • Sources/PostgREST/PostgrestTransformBuilder.swift
  • Tests/HelpersTests/PostgrestErrorTests.swift
  • Tests/PostgRESTTests/PostgrestBuilderTests.swift
  • dictionary.txt
  • sdk-compliance.yaml

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • maybeSingle() now returns nil only when no rows match.
    • Multiple matching rows correctly return a PGRST116 error.
  • Improvements

    • PostgREST errors now expose detailed information through details.
    • Existing detail usage remains supported for compatibility.
  • Documentation

    • Clarified maybeSingle() behavior for zero and multiple matching rows.

Walkthrough

PostgrestError now uses details as its stored property and JSON key. Deprecated detail accessors preserve compatibility. maybeSingle() parses PGRST116 details and returns nil only for zero rows. Multiple-row responses throw PostgrestError. Tests cover decoding, encoding, compatibility, zero rows, and multiple rows. Documentation and SDK compliance metadata were updated.

Sequence Diagram(s)

sequenceDiagram
  participant PostgrestBuilder
  participant PostgREST
  participant PostgrestError
  PostgrestBuilder->>PostgREST: Execute maybeSingle request
  PostgREST-->>PostgrestBuilder: Return data or PGRST116 error
  PostgrestBuilder->>PostgrestError: Parse details
  PostgrestError-->>PostgrestBuilder: Identify zero-row or multiple-row result
  PostgrestBuilder-->>PostgrestBuilder: Return nil or rethrow error
Loading

Assessment against linked issues

Objective Addressed Explanation
Distinguish zero-row and multiple-row PGRST116 responses in maybeSingle() [#1170]
Preserve public API compatibility while adopting Swift naming [#1170]
Add tests and update documentation [#1170]

Out-of-scope changes

Code Change Explanation
Add pkey to the dictionary (dictionary.txt:126) This does not relate to maybeSingle() error handling or the PostgrestError API.

Possibly related PRs

  • supabase/supabase-swift#1159: Shares the PostgrestError.details migration, compatibility APIs, tests, dictionary entry, and SDK compliance updates.

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.

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.

parity(postgrest): throw on multi-row result from maybeSingle when throw-on-error is enabled [from supabase-js]

1 participant