© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•8mo ago•
14 replies
Polaris

Error when insert in table "profiles" with edge-function and SERVICE_ROLE_KEY

I'm confused today.
I have an edge function that inserts into a table with RLS enabled.

I thought I read in the documentation that if I create a client with the SERVICE_ROLE_KEY key, RLS are ignored/overridden.
This doesn't seem to be the case, given my error:

"{\n code: "42501",\n details: null,\n hint: null,\n message: 'new row violates row-level security policy for table "profiles"'\n} {"code":"42501","details":null,"hint":null,"message":"new row violates row-level security policy for table \"profiles\""}\n"

Do you have any leads on this? I've been stuck on this for several days. My edge function:
const supabase = createClient(
  Deno.env.get("SUPABASE_URL")!,
  Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!, // Admin key
)

Deno.serve(async (req) => {

    .... Previous code without bug

    const {data: newProfile, error: errorProfile} = await supabase.from('profiles').insert({
         id: ID_AUTH,
         email: newUser.email,
         first_name: newUser.first_name,
         last_name: newUser.last_name,
         photo_url: `https://ui-avatars.com/api/?size=128&background=random&name=${newUser.first_name}+${newUser.last_name}`,
  })
  if(errorProfile){
    // Si erreur pendant la création du profil on supprime également l'authentification
    console.log(errorProfile, JSON.stringify(errorProfile))
    await supabase.auth.admin.deleteUser(ID_AUTH)
    return new Response('Error creating profile', {status: 400, headers: corsHeaders})
  }

})
const supabase = createClient(
  Deno.env.get("SUPABASE_URL")!,
  Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!, // Admin key
)

Deno.serve(async (req) => {

    .... Previous code without bug

    const {data: newProfile, error: errorProfile} = await supabase.from('profiles').insert({
         id: ID_AUTH,
         email: newUser.email,
         first_name: newUser.first_name,
         last_name: newUser.last_name,
         photo_url: `https://ui-avatars.com/api/?size=128&background=random&name=${newUser.first_name}+${newUser.last_name}`,
  })
  if(errorProfile){
    // Si erreur pendant la création du profil on supprime également l'authentification
    console.log(errorProfile, JSON.stringify(errorProfile))
    await supabase.auth.admin.deleteUser(ID_AUTH)
    return new Response('Error creating profile', {status: 400, headers: corsHeaders})
  }

})

Thanks for help
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Edge Function cannot insert into table with RLS even using Service Role Key
SupabaseSSupabase / help-and-questions
5mo ago
Using service role in Edge Function
SupabaseSSupabase / help-and-questions
7mo ago
Ensure edge function is invoked only with valid service-role key
SupabaseSSupabase / help-and-questions
4y ago
Cannot access new service role API key from Edge Function runtime
SupabaseSSupabase / help-and-questions
3mo ago