How do I create a trigger to make sure `created_at...
# help
j
How do I create a trigger to make sure
created_at
is not changed by the user? I'm thinking:
Copy code
typescript
create trigger posts_created_at after update on posts
OLD.created_at === NEW.created_at or undo transaction?
I'm not sure how to confirm this...
s
How I handle this is by using a trigger and making sure I reset this value before the update happens.
Copy code
sql
CREATE FUNCTION public.handle_created_at()
RETURNS TRIGGER AS $$
BEGIN
    new.created_at = old.created_at;
    return new;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

-- trigger the function every time a list is updated
CREATE TRIGGER on_list_updated
BEFORE UPDATE ON public.[table_name]
FOR EACH ROW EXECUTE PROCEDURE public.handle_created_at();
j
Thanks! Do I need the
SECURITY DEFINER
since we don't have any grant privilege options in supabase yet? (EDIT) - NM - this explains it -

https://www.youtube.com/watch?v=0N6M5BBe9AE&list=PL5S4mPUpp4OtkMYxAxNpEuuDQuI2pWF3-&index=3