Supabase函数不进行任何计算。

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

Supabase function doesn't do any calculations

问题

我有一个函数:

  1. begin
  2. if pg_trigger_depth() <> 1 then
  3. return new;
  4. end if;
  5. new.rating = ROUND((new.total_score / new.total_reviews), 1);
  6. return new;
  7. end;

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

route定义:

  1. create table
  2. public.route (
  3. id bigint generated by default as identity not null,
  4. name text null,
  5. country text null,
  6. city text null,
  7. description text null,
  8. imageName text null,
  9. created_at timestamp with time zone null default now(),
  10. total_reviews numeric null,
  11. total_score numeric null,
  12. duration numeric not null default '30'::numeric,
  13. rating double precision null,
  14. isAdult boolean not null default false,
  15. constraint route_pkey primary key (id)
  16. ) tablespace pg_default;
  17. create trigger update_route_rating_trigger
  18. after insert
  19. or
  20. update on route for each row
  21. execute function update_route_rating ();

route上的RLS策略:


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

目标角色:public

使用表达式:true

使用检查表达式:true


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

目标角色:public

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


英文:

I have a function:

  1. begin
  2. if pg_trigger_depth() <> 1 then
  3. return new;
  4. end if;
  5. new.rating = ROUND((new.total_score / new.total_reviews), 1);
  6. return new;
  7. 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:

  1. create table
  2. public.route (
  3. id bigint generated by default as identity not null,
  4. name text null,
  5. country text null,
  6. city text null,
  7. description text null,
  8. imageName text null,
  9. created_at timestamp with time zone null default now(),
  10. total_reviews numeric null,
  11. total_score numeric null,
  12. duration numeric not null default '30'::numeric,
  13. rating double precision null,
  14. isAdult boolean not null default false,
  15. constraint route_pkey primary key (id)
  16. ) tablespace pg_default;
  17. create trigger update_route_rating_trigger
  18. after insert
  19. or
  20. update on route for each row
  21. 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:

确定