Files
supabase/examples
JerryandNik Richers 3389756a4c fix(docs): correct the JWT team policy example (#53777)
## Problem

Fixes #53776

We hit this while adding team-scoped RLS to a project from the guide.
Creating the policy fails with:

```text
ERROR:  operator does not exist: uuid = jsonb
```

The example uses `team_id in (select auth.jwt() -> 'app_metadata' ->
'teams')`. `->` returns a single `jsonb` value, so `in (select ...)`
compares `team_id` to that jsonb value.

## Solution

Expand the claim with `jsonb_array_elements_text`, cast each team ID to
`uuid`, and compare it with `= any (array(...))`:

```sql
using (
  team_id = any (
    array(
      select (jsonb_array_elements_text((select auth.jwt()) -> 'app_metadata' -> 'teams'))::uuid
    )
  )
);
```

`app_metadata.teams` must be an array of UUID strings, for example
`{"app_metadata": {"teams": ["22222222-2222-2222-2222-222222222222"]}}`.
Objects such as `[{"id": "..."}]` fail the cast.

Comparing UUID to UUID means uppercase values in the claim still match
and Postgres can use an index on `team_id`. The guide and the AI prompt
in `examples/prompts/database-rls-policies.md` both document the
expected claim shape and that `team_id` is a `uuid`; for other ID types,
cast the array element to that type instead.

## Review instructions

1. Open the `auth.jwt()` section of the [RLS
guide](https://supabase.com/docs/guides/database/postgres/row-level-security#auth-jwt).
2. Run the updated `create policy` statement against Postgres. It should
create without error.
3. Check that a user whose `app_metadata.teams` is an array of UUID
strings containing the row's `team_id` sees the row, including when the
claim uses an uppercase UUID, and that a user without it does not.

I checked these steps on Postgres 14.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)

I used AI to help debug this. The fix and this pull request are mine; I
reviewed the change before opening it.

---------

Co-authored-by: Nik Richers <nrichers@gmail.com>
2026-11-24 22:20:38 +00:00
..