Skip to content

fix(postgrest): handle maybe_single zero-row responses correctly#1401

Closed
AhmedAli58 wants to merge 1 commit intosupabase:mainfrom
AhmedAli58:fix/postgrest-maybe-single-204
Closed

fix(postgrest): handle maybe_single zero-row responses correctly#1401
AhmedAli58 wants to merge 1 commit intosupabase:mainfrom
AhmedAli58:fix/postgrest-maybe-single-204

Conversation

@AhmedAli58
Copy link

Summary

Fixes #1207.

maybe_single() now treats zero-row responses returned as HTTP 204 (No Content) as an empty-success case and returns SingleAPIResponse(data=None) instead of raising APIError.

What changed

  • Updated SyncMaybeSingleRequestBuilder.execute() and AsyncMaybeSingleRequestBuilder.execute() to treat 204/no-row outcomes as data=None.
  • Added a small helper in both builders to recognize no-row conditions from either:
    • explicit 204 errors
    • legacy "The result contains 0 rows" error details
  • Normalized the parsed empty-body [] path in maybe_single() to data=None.

Behavior guarantees

  • maybe_single() on zero rows: returns response object with data=None.
  • single() behavior is unchanged: zero-row error responses still raise APIError.
  • Non-zero-row error handling remains unchanged.

Tests

Added regression coverage in both sync and async client tests:

  • test_maybe_single_returns_none_on_no_rows_204
  • test_single_raises_on_no_rows

Verification

  • make ruff passed
  • uv run --package postgrest mypy src/postgrest tests passed
  • uv run --package postgrest pytest src/postgrest/tests/_sync/test_client.py src/postgrest/tests/_async/test_client.py -vv passed
  • uv run --package postgrest pytest src/postgrest/tests -vv -k "not integration" passed

Note: make postgrest.tests could not run integration tests in this environment because docker is not installed (/bin/sh: docker: command not found).

@coderabbitai
Copy link

coderabbitai bot commented Feb 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Cache: Disabled due to Reviews > Disable Cache setting

Disabled knowledge base sources:

  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 59e3384 and 79a64a8.

📒 Files selected for processing (4)
  • src/postgrest/src/postgrest/_async/request_builder.py
  • src/postgrest/src/postgrest/_sync/request_builder.py
  • src/postgrest/tests/_async/test_client.py
  • src/postgrest/tests/_sync/test_client.py

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Fixed maybe_single() queries to gracefully handle no-row responses by returning None for the data field instead of raising an error.
    • Single queries maintain their strict behavior by raising errors when no matching rows are found.
  • Tests

    • Added comprehensive test coverage for no-row edge cases in both single and maybe_single query types.

Walkthrough

This pull request fixes a bug where maybe_single() incorrectly raises an APIError when a query returns zero rows. The fix introduces private helper functions and constants to detect "no rows" error responses in both async and sync request builders. When a "no rows" error is detected, instead of propagating the error, the code now returns a SingleAPIResponse with data=None. Additionally, when a successful single request returns an empty list, it is converted to return data=None while preserving the count. Corresponding test cases are added to verify the corrected behavior for both async and sync implementations.

Assessment against linked issues

Objective Addressed Explanation
maybe_single() should return APIResponse(data=None) when 0 rows are returned instead of raising APIError [#1207]

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 and usage tips.

@o-santi
Copy link
Contributor

o-santi commented Mar 10, 2026

This would be a breaking change, so we won't be doing it for v2. I'll be fixing this in the next major version myself, you can follow it's development here #1370.

@o-santi o-santi closed this Mar 10, 2026
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.

maybe_single throws an error when 0 rows are returned

3 participants