You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成Supabase时数据库UPDATE操作失败求助

Flutter Supabase UPDATE操作失败求助

已基于《Flutter Database & User Authentication Quickstart》视频完成Google登录功能开发,但在更新Supabase的profiles表时始终失败,相关信息如下:

Flutter更新代码

final userId = supabase.auth.currentUser!.id;
final username = _nameController.text.trim();

await supabase.from('profiles').update({
     'username': username,
     // 存储地点名称的列表
     'places': userPlacesList,
}).eq('id', userId);

Supabase SQL配置

create table profiles (
  id uuid references auth.users not null primary key,
  updated_at timestamp with time zone,
  username text unique,
  avatar_url text,
  places text[],

  constraint username_length check (char_length(username) >= 1)
);

alter table profiles
  enable row level security;

create policy "Public profiles are viewable by everyone." on profiles
  for select using (true);

create policy "Users can insert their own profile." on profiles
  for insert with check (auth.uid() = id);

create policy "Users can update own profile." on profiles
  for update using (auth.uid() = id);


create function public.handle_new_user()
returns trigger as $$
begin
  insert into public.profiles (id, username, avatar_url)
  values (new.id, new.raw_user_meta_data->>'username', new.raw_user_meta_data->>'avatar_url');
  return new;
end;
$$ language plpgsql security definer;
create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();

已排查操作

  • 将「Users can update own profile.」策略的目标角色从Default改为authenticated,问题未解决
  • 怀疑handle_new_user函数可能存在问题

表结构与策略确认

  • 表结构:profiles表包含id(uuid类型主键,关联auth.users)、updated_at、username(唯一约束)、avatar_url、places(text数组类型)字段,且有username长度至少为1的检查约束
  • 行级安全策略:
    • 允许所有用户查询profiles表
    • 仅允许用户插入自己的profile记录
    • 仅允许用户更新自己的profile记录

内容的提问来源于stack exchange,提问作者atom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 22:34:54