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
相关产品推荐
相关产品推荐

