AI Resource Pro / website / README-BLOG-FIX.md
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:
- Policy for
userstable checks if a user is an admin by querying theuserstable - This creates a circular reference that Postgres can't resolve
- 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:
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
- Log in to your Supabase dashboard
- Navigate to the SQL Editor
- Copy the contents of
scripts/create-exec-sql-function.sql - Paste it into the SQL Editor and run it
- 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:
npm run supabase:fix-recursion
Alternatively, you can:
- Copy the contents of
scripts/fix-recursive-policies.sql - Paste it into the Supabase SQL Editor and run it directly
What the Fix Does
The fix:
- Drops problematic policies that cause recursion
- Creates a
is_admin()function that safely checks admin status - Recreates policies using this function to avoid recursion
- Applies the fix to all affected tables
Verifying the Fix
After applying either solution, you can verify it worked:
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=trueduring development - Set it to
falsein 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.