0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SupabaseでInsertできなかった①

0
Posted at

よくあるので①としています。
よくあるけど書くかはわかりません。

構成

  • Next.js
  • Supabase

事象

テーブル構成はマスタ+権限(auth.usersとの中間テーブルでもある)
マスタを作成したら最小限の権限も同時に作成されます。
マスタテーブルにInsertしたかったけどできなかった。
RLSは想定通りに設定されていた。

↓が問題のコード

// 生成されるidが欲しくて取得もしていた
const { data, error } = await supabase
    .from("マスタ")
    .insert({
        hoge,
        fuga
    } as Partial<MasterRow>)
    .select("id")
    .single();

RLS

マスタテーブル

create policy "マスタのインサートポリシー" on public.マスタ
for insert with check (
  auth.role() = 'authenticated' and 内緒の条件
);

create policy "マスタのセレクトポリシー" on public.マスタ
for select using (
  権限を参照してる条件
);

権限テーブル

create policy "権限のポリシー" on public.権限
for all using (
  内緒の条件
) with check (
  内緒の条件
);

結論

selectがダメだった。
同時のselectは権限が作成されてないから通らない。

+ const id = randomUUID();
  const { data, error } = await supabase
      .from("マスタ")
      .insert({
+         id,
          hoge,
          fuga
      } as Partial<MasterRow>)
-     .select("id")
-     .single();

RLSで転けてる時はしんどい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?