Supabase function work only when added in dbms, but not through query

66 Views Asked by At

I have two tables, feedback and quests.

feedback table:

create table
  public.feedback (
    id bigint generated by default as identity,
    created_at timestamp with time zone not null default now(),
    user_id uuid null,
    quest bigint null,
    comment text null,
    rating numeric null,
    constraint feedback_pkey primary key (id),
    constraint feedback_quest_fkey foreign key (quest) references quests (id) on delete cascade,
    constraint feedback_user_id_fkey foreign key (user_id) references profile (id) on delete cascade
  ) tablespace pg_default;

create trigger update_quest_data_on_feedback before insert on feedback for each row
execute function update_quest_data ();

create trigger insert_feedback_duplicate before insert on feedback for each row
execute function check_duplicate_feedback ();

quests table:

create table
  public.quests (
    id bigint generated by default as identity,
    name text null,
    description text null,
    city text null,
    cover text null,
    created_at timestamp with time zone not null default now(),
    price numeric null,
    total_reviews numeric null,
    total_score numeric null,
    rating numeric(5, 2) null,
    duration numeric null,
    players text null,
    constraint quests_pkey primary key (id)
  ) tablespace pg_default;

And I have a trigger on table feedback on INSERT, before event, for each row. That trigger fires function:

BEGIN
  UPDATE quests
  SET total_reviews = total_reviews + 1,
      total_score = total_score + NEW.rating
  WHERE id = NEW.quest;
  RETURN NEW;
END;

Function returns trigger.

That means, that every time I add a new row to feedback, function must update quests data, total_reviews must be +1 and total_score must be total_score + NEW.rating(This is the rating, that user sends), where id equals NEW.quest.

But there is a problem. If I add new row to feedback via JS query on client-side when user send the feedback, supabase add the row to feedback, but don't update the quest rating, but if I add a new row to feedback via supabase site, by my own, supabase add the new row to feedback AND update the quest rating. I have no idea why, please help.

1

There are 1 best solutions below

0
On

The solve of this issue is add UPDATE RLS Policy to table quests. I just forgot about it.

When you add a new row to feedback update function is working, cuz it triggered with admin privs. But when you triggers this function via JS you don't have admin privs, that's why you can insert new row to feedback, but can't update the quests