【问题标题】:Supabase update with row level policies使用行级策略进行 Supabase 更新
【发布时间】:2021-10-09 03:57:27
【问题描述】:

我无法更新启用角色级别策略的行

我的表有如下插入和更新的行级策略:

create policy "Allow individual insert access" on public.quotes for insert with check ( auth.uid() = created_by );

create policy "Allow individual update access" on public.quotes for update with check ( auth.uid() = created_by );

一旦用户登录,我的插入功能就可以正常工作:

export const addQuote = async (user_id, content) => {
try {
let body = await supabase
  .from("quotes")
  .insert([{ created_by: user_id, content: content }]);
return body;
} catch (error) {
console.log("error", error);
}
};

我的更新功能在没有行级策略的情况下工作正常:

export const updateQuote = async (quote_id, user_id, new_content) => {
try {
let body = await supabase
  .from("quotes")
  .update([{ created_by: user_id, content: new_content }])
  .eq("id", quote_id);
return body;
} catch (error) {
console.log("error in updateQuote", error);
}
};

但开启 RLS 时响应为 404 且行未更新。

我在这里错过了什么?

【问题讨论】:

    标签: row-level-security supabase


    【解决方案1】:

    我将行级策略从 with check 更改为 using,它按预期工作。

    create policy "Allow individual update access" on public.quotes for update using( auth.uid() = created_by );
    

    使用检查更新 ---> 使用更新

    【讨论】:

      【解决方案2】:

      您的数据周围似乎有额外的[]。没有它你可以试试吗?

      let body = await supabase
        .from("quotes")
        .update({ created_by: user_id, content: new_content })
        .eq("id", quote_id);
      

      【讨论】:

      • 确实,我删除了数组,但没有帮助。我的脚趾水平政策是错误的。检查我的答案。
      • @Olivier 不错!很高兴听到您能够让它工作!
      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 2022-10-02
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      相关资源
      最近更新 更多