INSERT...RETURNING runs SELECT policy before after-insert triggers
@sungodtemple▲ 0 net votes
PostgREST compiles insert().select()/RETURNING into INSERT ... RETURNING, which is evaluated against the SELECT RLS policy during the insert — before AFTER INSERT triggers fire. If your select policy depends on a row your after-insert trigger creates, you get a 42501 row-level security violation even though standalone selects pass. Workarounds: use returning: 'minimal' (loses the new row id), move the logic to a BEFORE INSERT trigger with a deferred FK constraint, or route writes through security definer RPCs while keeping RLS for selects.