Supabase函数不进行任何计算。

huangapple go评论66阅读模式
英文:

Supabase function doesn't do any calculations

问题

我有一个函数:

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

每当我在route表上进行更新时,此函数必须更新表route中的rating列。现在查询在触发,total_scoretotal_reviews正在更新,但rating列保持不变。我的意思是,它没有进行计算,永远不会更改。不管我在所需列中有什么值。

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 ();

route上的RLS策略:


策略名称:允许在路线上更新

目标角色:public

使用表达式:true

使用检查表达式:true


策略名称:授权用户可以从route表中选择

目标角色:public

使用表达式:(auth.role() = 'authenticated'::text)


英文:

I have a function:

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

This function must update column rating in table route every time I make an update on route table. Now the query if firing, total_score and total_reviews is getting updates, but column rating stays the same. I mean, there is no calculation on it and it's never change. No matter what's values I have in needed columns.

table route definition:

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 Policies on route:


Policy name: allow_update_on_route

Target roles: public

USING expression: true

WITH CHECK expression: true


Policy name: Authorized users can select from route table

Target roles: public

USING expression: (auth.role() = 'authenticated'::text)


答案1

得分: 1

正如@Belayer所说,我的解决方案不起作用,因为它具有“行后触发器”,行后触发器不能更改数据值,所以这就是为什么在这个解决方案中,我应该使用“行前触发器”。

此外,我认为最有效的解决方案是使用“生成列”,就像@dshukertjr所说的那样。

这是一个生成列文档的链接,以供参考:postgresql.org/generated-columns

英文:

As @Belayer said, my solution won't work cuz it has after row trigger and an after trigger cannot change data values, so that's why in this solution, I should have used before row trigger.

Also, the best working solution (IMHO) is a generated-columns, as @dshukertjr said.

Here is a link to generated columns doc for help: postgresql.org/generated-columns

huangapple
  • 本文由 发表于 2023年6月2日 09:34:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定