Search code examples
postgresqlsupabasesupabase-database

Supabase: How to automatically update a timestamp field after updating a row?


What DB I am using?

  • Supabase hosted version

What do I need?

  • After I update a row with .update({ name: 'Middle Earth' }) method I need to automatically update also a timestamp in my table.

How can I update automatically a timestamp?


Solution

  • 1)If you already have a table use this script (provided by Supabase devs themselves):

    create extension if not exists moddatetime schema extensions;
    
    -- assuming the table name is "todos", and a timestamp column "updated_at"
    -- this trigger will set the "updated_at" column to the current timestamp for every update
    create trigger handle_updated_at before update on todos
      for each row execute procedure moddatetime (updated_at);
    

    2)What if I don't wanna use the moddatetime extension?

    This stackoverflow question will give you an answer.