Lovable + Supabase: “infinite recursion detected in policy for relation”
Last verified: October 18, 2026
infinite recursion detected in policy for relation "profiles"
What it means
A Row Level Security policy reads the same table it protects (for example an “admins can read all profiles” policy that looks up profiles.role), or two tables’ policies read each other. Postgres detects the loop and fails the query with error 42P17, which the API returns as HTTP 500.
How to fix it
- Open Supabase → Authentication → Policies (or Table Editor → the table → Policies) and find the policy on the table named in the error that queries that same table, or a table whose policy reads back.
- Move the lookup into a
security definerfunction in a private (non-exposed) schema, e.g.private.is_admin(), withset search_path = ''and fully qualified table names. - Rewrite the policy to call the function:
using ((select private.is_admin())). Do not disable RLS to make the error go away. - Test as a normal user and as an admin. Both should load without the error and see only what they should.
If your Lovable project uses Lovable Cloud instead of your own Supabase project, the same database lives under More → Cloud → Database. Its RLS view is read-only, so ask Lovable in chat (Plan mode) to make the policy change.
Stop the loop: paste these into Lovable
1. Diagnose first
Stop editing code and do not retry previous fixes. Supabase returns: infinite recursion detected in policy for relation "profiles" (42P17). Without changing anything, list every RLS policy on the table in the error and on any table those policies query. Tell me which policy reads its own table or creates a cycle between two tables. Report the root cause in one sentence and propose ONE minimal change.
2. Then the one fix
Fix the recursive RLS policy the Supabase-recommended way: create a security definer function in a private schema (not exposed to the API) that does the lookup, with set search_path = '' and schema-qualified names, grant execute to authenticated, and change only the recursive policy to call it, e.g. using ((select private.is_admin())). Keep RLS enabled on every table. Do not use the service role key in the browser. Then test as a regular user and an admin and tell me exactly what SQL you ran.
3. Save this rule so it stays fixed
Lovable: Project settings → Knowledge. Cursor: .cursor/rules or .cursorrules. Bolt: Project settings → Knowledge. Replit: replit.md.
Rule: RLS policies must never query the table they protect, and two tables' policies must never read each other. Put role or membership lookups in a security definer function in the private schema (search_path = '') and call it from the policy. Never fix RLS errors by disabling RLS.
FAQ
Can I just turn RLS off?
No. With RLS off, anyone with your public key can read or change the table. Fix the policy instead.
Why did it start after adding an admin role?
Admin checks are the most common cause: the policy on profiles looks up profiles.role, which triggers the same policy again.
Sources
- Supabase troubleshooting: RLS policy causes infinite recursion
- Supabase docs: Row Level Security (security definer functions, avoid recursive policies)
- PostgREST error codes (42P17 → 500)
Your error is a bit different? Paste it with what you already tried and get a diagnose-first prompt, one fix and a rule for your exact case.
Break the loop — 3 free fixes Still stuck? Get a human fix