英文:
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_score
和total_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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论