Skip to content

Schema switching fails in supabase-js #726

@rbrueckner

Description

@rbrueckner

Bug report

Describe the bug

I run Supabase in a self-hosted manner using Docker.
I create a Supabase client using the supabase-js v2 lib and a custom postgres schema.
When I try to fetch data from a table under that custom schema I get the following error:
{
code: 'PGRST106',
details: null,
hint: null,
message: 'The schema must be one of the following: public, storage, graphql_public'
}

However, the schema is added to the environment via
PGRST_DB_SCHEMAS=public,storage,graphql_public,custom_schema
and fetching data via the API interface using curl works:

curl -X GET 'https://.de/rest/v1/patterns?select=*' -H "Accept-Profile: custom_schema" -H "apikey: <anon_key>" -H "Authorization: Bearer <anon_key>"
[{"id":1,"title":"Test","published":false,"created_at":"2023-03-12T15:45:39.407648+00:00","updated_at":"2023-03-12T15:45:39.407648+00:00"}]

To Reproduce

Database:

CREATE SCHEMA IF NOT EXISTS custom_schema;

grant usage on schema custom_schema to postgres, supabase_admin, anon, authenticated, service_role;
alter default privileges in schema custom_schema grant all on tables to postgres, anon, authenticated, service_role;
alter default privileges in schema custom_schema grant all on functions to postgres, anon, authenticated, service_role;
alter default privileges in schema custom_schema grant all on sequences to postgres, anon, authenticated, service_role;

CREATE TABLE IF NOT EXISTS custom_schema.patterns (
id bigint generated by default as identity primary key,
title text NOT NULL,
published boolean DEFAULT false,
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now()
);

create index on custom_schema.patterns using gin (tags);

grant select on table custom_schema.patterns to postgres, supabase_admin, anon, authenticated, service_role;
grant usage, select on all sequences in schema custom_schema to postgres, authenticated, service_role, supabase_admin, anon;

  • manually added one test row to the database after creation via Supabase Studio

Client:

import { createClient } from '@supabase/supabase-js'

const supabase = createClient('https://.de', process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY, {db: { schema: 'custom_schema' }})

export default async function Page() {
const { data, error } = await supabase.from('patterns').select()
console.log(data)

return (
<div>Patterns</div>
)}

Docker - supabase.env:
PGRST_DB_SCHEMAS=public,storage,graphql_public,custom_schema

I debugged/traced the error down to l. 84 in PostgrestBuilder.ts in @supabase/postgrest-js:
const body = await res.text()

The req header DOES contain the schema switch entry "Accept-Profile: custom_schema" and the setup seems ok to me, but the response is wrong!

Expected behavior

I would expect the same output in the data object (-> also via console.log) as using curl on the CLI

System information

  • OS: Windows or Linux (tried both)
  • Browser Chrome, Edge
  • Version of supabase-js: 2.10.0
  • Version of Node.js: 18.13.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions