# Blog API Fix: Infinite Recursion in Policies This README provides instructions on how to fix the "infinite recursion detected in policy for relation 'users'" error in your Supabase database. ## The Problem The Supabase database has row-level security (RLS) policies that are causing an infinite recursion error. This happens because: 1. Policy for `users` table checks if a user is an admin by querying the `users` table 2. This creates a circular reference that Postgres can't resolve 3. The error propagates to other tables with similar policy structures ## Solution 1: Immediate API Fix (Already Implemented) We've already modified the `/api/blog` API route to: - Use the service client when in development mode (`BYPASS_AUTH_IN_DEV=true`) - Filter published posts manually when needed - Bypass the recursive policy issue entirely This change should be in effect as soon as you restart your Next.js server: ```bash npm run dev ``` ## Solution 2: Permanent Database Fix To fix the issue permanently in your database, follow these steps: ### Step 1: Create the exec_sql Function 1. Log in to your [Supabase dashboard](https://app.supabase.com/) 2. Navigate to the SQL Editor 3. Copy the contents of `scripts/create-exec-sql-function.sql` 4. Paste it into the SQL Editor and run it 5. This creates a function that can execute SQL statements ### Step 2: Run the Fix Script After creating the function, you can run the fix script: ```bash npm run supabase:fix-recursion ``` Alternatively, you can: 1. Copy the contents of `scripts/fix-recursive-policies.sql` 2. Paste it into the Supabase SQL Editor and run it directly ## What the Fix Does The fix: 1. Drops problematic policies that cause recursion 2. Creates a `is_admin()` function that safely checks admin status 3. Recreates policies using this function to avoid recursion 4. Applies the fix to all affected tables ## Verifying the Fix After applying either solution, you can verify it worked: ```bash npm run debug:direct-query ``` You should see that both the service client and regular client can successfully query blog posts. ## Further Information - Keep `BYPASS_AUTH_IN_DEV=true` during development - Set it to `false` in production for security - You can check blog posts in the database with: ```bash npm run check:blog-posts ``` - You can publish the hello-world sample post with: ```bash npm run publish:hello-world ``` If you need additional help, please refer to the Supabase documentation on [Row Level Security](https://supabase.com/docs/guides/auth/row-level-security).