javascript Supabase不接受UPDATE查询

toiithl6  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(153)

我目前正在使用数据库,使快速MVP的大学工作。我创建了反馈功能,所以我必须更新行中的一些列。下面是我的JS查询:

const { error: updateError } = await supabase.from('route').update({ total_score: 5 }).eq('id', 1);

获取此错误:开发工具中的错误代码:400
如果你不想看截图:{“code”:“21000”,
“details”:null,
“hint”:null,
“message”:“UPDATE需要WHERE子句”}
并且在此查询之后在supabase处零更新。我不知道该怎么办。不久前我在项目中使用了这个查询,它很好。但在这里-不。
有办法解决吗?
UPD:调用和更新查询的函数:

async function setRating() {
    const rate = range.value.value
    const { error: updateError } = await supabase.from('route').update({ total_score: 5 }).eq('id', 1);

    console.log(updateError);
    // Commented this section cuz update query above doesn't work
    // if (!update_error) {
    //   console.log(update);
    //   route_feedback.value.classList.add('hide')
    //   route_ended.value.classList.add('show')
    //   setTimeout(() => {
    //     route_feedback.value.classList.remove('show')
    //     route_feedback.value.classList.remove('hide')
    //   }, 500);
    // }
  }
  }

在supabase中更新的RLS策略:
策略名称:allow_update_on_route
目标角色:公众
使用表达式:真的
WITH CHECK表达式:真的
策略名称:授权用户可从路由表中选择
目标角色:公众
使用表达式:(auth.role()= 'authenticated'::text)
UPD 2:表route定义:

create table
  public.route (
    id bigint generated by default as identity not null,
    name text null,
    country text null,
    city text null,
    description text null,
    imageName text null,
    created_at timestamp with time zone null default now(),
    total_reviews numeric null,
    total_score numeric null,
    duration numeric not null default '30'::numeric,
    rating double precision null,
    isAdult boolean not null default false,
    constraint route_pkey primary key (id)
  ) tablespace pg_default;

create trigger update_route_rating_trigger
after insert
or
update on route for each row
execute function update_route_rating ();

没有RLS,仍出现相同错误。
update_route_rating函数定义:

BEGIN
    IF pg_trigger_depth() <> 1 THEN
        RETURN NEW;
    END IF;
   UPDATE route SET rating = ROUND((total_score / total_reviews), 1);
   return new;
END;
n9vozmp4

n9vozmp41#

您设置为触发器的update_route_rating()函数是此处的原因。
触发器函数中的以下语句试图更新route表中的每一行,因为它没有任何where子句。

UPDATE route SET rating = ROUND((new.total_score / new.total_reviews), 1);

我假设您想要在这里实现的是为新插入/更新的行更新rating列。在这种情况下,您可以在触发器函数中使用以下内容。

begin
    if pg_trigger_depth() <> 1 then
        return new;
    end if;
   new.rating = ROUND((total_score / total_reviews), 1);
   return new;
end;

相关问题