postgresqlsupabasesupabase-databasesupabase-js

Local Edge Function gives error Supabase relation "public.<table_name>" does not exist


I'm trying to develop a local supabase edge function. I have followed the documentation and correctly installed and locally deployed the function.

I have a simple edge function that, when it receive a request, it fetches some data from the database:

import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.45.4'

Deno.serve(async (req) => {
  try {
    const supabase = createClient(
      Deno.env.get('SUPABASE_URL') ?? '',
      Deno.env.get('SUPABASE_ANON_KEY') ?? '',
      { global: { headers: { Authorization: req.headers.get('Authorization')! } } }
    )
    // Fetch all websites from the 'websites' table
    const { data: websites, error } = await supabase
      .from('websites')
      .select('url')
      .limit(1)
    
    if (error) {
      console.log(error)
      throw error
    }

I use supabase function serve to test locally but, everytime I launch http://127.0.0.1:54321/functions/v1/<function-name> to test it I have the error

{"error":"relation \"public.<table_name>\" does not exist"}

I have also tried to change the SUPABASE_ANON_KEY with the SUPABASE_SERVICE_ROLE_KEY but same issue

The table public.<table_name> exist on my cloud instance of Supabase

I have also tried to disable RLS but still the same issue.

I have also tried to follow this simple example but it fails...

Running the SQL questy directly on Supabase cloud works SELECT <column_name> FROM public.<table_name>

I think there's some issue with the local edge function


Solution

  • In your code you are using Deno.env.get('SUPABASE_URL') which will call the local supabase url and not your cloud hosted one. You would need to set a .env file inside of your supabase/functions directory with the SUPABASE_URL to your cloud hosted endpoint for this to work with your cloud hosted supabase instance. Do note that you will also need to set the SUPABASE_ANON_KEY in your .env file to match that of the cloud hosted one too.