Skip to content

feat(postgrest): query a table in the schema its model declares - #1030

Merged
Tr00d merged 2 commits into
masterfrom
feat/postgrest-table-schema
Dec 2, 2026
Merged

Tr00d merged 2 commits into
masterfrom
feat/postgrest-table-schema

Conversation

@Tr00d

@Tr00d Tr00d commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Summary

A model can name the schema its table lives in, [Table("stock", Schema = "inventory")], and every request on that table targets it, so tables from several schemas work side by side on one client. This is groundwork for generated types (database.generated_types.typed_queries), whose tables carry their schema; it carries no capability annotation.

Public API

// TableAttribute
public string? Schema { get; init; }
[Table("stock", Schema = "inventory")]
public class Stock : BaseModel { [PrimaryKey("id")] public int Id { get; set; } }

var stock = await client.Table<Stock>().Get();                          // Accept-Profile: inventory
var archived = await client.WithSchema("archive").Table<Stock>().Get(); // Accept-Profile: archive

Behavior

  • The schema of a table is resolved once, when the table is built, in this order:

    1. the schema of a client derived with WithSchema(...);
    2. TableAttribute.Schema, when it is neither null nor empty;
    3. ClientOptions.Schema.

    A deliberate per-client scope wins over the table's home, so one table shape can be queried in several schemas (archive, schema per tenant); the table's home wins over the client-wide default, including a ClientOptions.Schema set at construction. This is the rule supabase-flutter applies to its table definitions (_schemaIsExplicit ? _schema : (table.schema ?? _schema)).

  • It reaches every request on the table through the usual profile header (Accept-Profile for GET and HEAD, Content-Profile otherwise): reads, Count, GetAsync<TResult> projections and writes, and TableWithCache and SupabaseTable, which build on Table. A model read through the table carries the same options, so its Update/Delete target the same schema.

  • A table with a schema of its own works on a copy of the client's options; the client's options are unchanged. WithSchema marks the options it derives as scoped, and the mark survives further derivations such as WithAccessToken.

  • Embedding across schemas is not possible in PostgREST: relationships are resolved within the schema of the request (PGRST200), so the schema of an embedded model is not used.

  • Failures are unchanged: a schema the API does not expose answers 406 PGRST106 (FailureHint.Reason.InvalidSchema).

Deviations from supabase-js

supabase-js has no per-table schema; a schema is chosen per client or with .schema(). The precedence above matches supabase-flutter.

Tests

Tier Class Pins
E2E TableSchemaTests A model of personal.users inserts, reads and updates there from a public client, and a WithSchema("public") client reads public.users instead
Contract TableSchemaRequestTests The profile header for each precedence case, on reads, counts, projections, inserts and deletes; client options unchanged

The tests failed before the change (8 of 15: every request targeted public).

Diff coverage: 100% (14 lines). Mutation: 8 killed of 8 tested on changed lines, 0 survivors.

Notes

  • RequestApprovalFixture answers with Content-Range: */0, so a count can be pinned through it.
  • TableAttribute.cs moved to a file-scoped namespace with its docs completed, as the format gate requires of a changed file.
  • The cache of a TableWithCache is kept per schema in its own fix (fix(postgrest): keep the cache of a table separate per schema #1029). The Realtime subscription of SupabaseTable still uses public; it follows in its own PR with a public IPostgrestTable.Schema.

@Tr00d
Tr00d marked this pull request as ready for review December 2, 2026 10:15
@Tr00d
Tr00d merged commit e99dffd into master Dec 2, 2026
11 checks passed
@Tr00d
Tr00d deleted the feat/postgrest-table-schema branch December 2, 2026 10:21
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.

1 participant