supabase / supabase-js

An isomorphic Javascript client for Supabase. Query your Supabase database, subscribe to realtime events, upload and download files, browse typescript examples, invoke postgres functions via rpc, invoke supabase edge functions, query pgvector.
https://supabase.com
MIT License
3.14k stars 251 forks source link

Schema switching fails in supabase-js #726

Open rbrueckner opened 1 year ago

rbrueckner commented 1 year ago

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: " -H "Authorization: Bearer " [{"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;

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 ( \

Patterns\
)}

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

rbrueckner commented 1 year ago

Add-on: I just tried replacing "custom_schema" with "public" (after creating the respective table in the public schema and populating it with example data) and even there get the same error! Again, things work correctly on the CLI.