-- Script to fix recursive policies in Supabase -- Open Supabase Dashboard > SQL Editor and execute this script -- This will fix the "infinite recursion detected in policy for relation 'users'" error -- First, drop the problematic policies that cause recursion DROP POLICY IF EXISTS "Admins can view all users" ON public.users; DROP POLICY IF EXISTS "Admins can update users" ON public.users; -- Create new policies that avoid recursion by using a different approach -- Instead of querying the users table within its own policy, we'll use a direct check -- Create a function to check if a user is an admin CREATE OR REPLACE FUNCTION is_admin(user_id uuid) RETURNS boolean AS $$ DECLARE admin_status boolean; BEGIN -- Direct query without using policies SELECT is_admin INTO admin_status FROM public.users WHERE id = user_id; RETURN COALESCE(admin_status, false); END; $$ LANGUAGE plpgsql SECURITY DEFINER; -- Create new policies using the function CREATE POLICY "Admins can view all users" ON public.users FOR SELECT USING ( is_admin(auth.uid()) ); CREATE POLICY "Admins can update users" ON public.users FOR UPDATE USING ( is_admin(auth.uid()) ); -- Fix other policies that might have the same issue DROP POLICY IF EXISTS "Admins can manage blog posts" ON public.blog_posts; DROP POLICY IF EXISTS "Admins can manage categories" ON public.categories; DROP POLICY IF EXISTS "Admins can manage tools" ON public.tools; DROP POLICY IF EXISTS "Admins can manage reviews" ON public.reviews; DROP POLICY IF EXISTS "Admins can manage news" ON public.news; DROP POLICY IF EXISTS "Admins can manage learning resources" ON public.learning_resources; -- Recreate them using the function CREATE POLICY "Admins can manage blog posts" ON public.blog_posts FOR ALL USING ( is_admin(auth.uid()) ); CREATE POLICY "Admins can manage categories" ON public.categories FOR ALL USING ( is_admin(auth.uid()) ); CREATE POLICY "Admins can manage tools" ON public.tools FOR ALL USING ( is_admin(auth.uid()) ); CREATE POLICY "Admins can manage reviews" ON public.reviews FOR ALL USING ( is_admin(auth.uid()) ); CREATE POLICY "Admins can manage news" ON public.news FOR ALL USING ( is_admin(auth.uid()) ); CREATE POLICY "Admins can manage learning resources" ON public.learning_resources FOR ALL USING ( is_admin(auth.uid()) );