A Postgres function to change the updated_at
column of a record on INSERT
/UPDATE
Created
January 5, 2017 04:58
-
-
Save colophonemes/5880ac77ac8a5e355e7329dea6854282 to your computer and use it in GitHub Desktop.
Postgres auto-updating timestamps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- The set_updated_at() function | |
CREATE FUNCTION set_updated_at() | |
RETURNS TRIGGER AS $$ | |
BEGIN | |
NEW.updated_at = now(); | |
RETURN NEW; | |
END; | |
$$ language 'plpgsql'; | |
-- Assigning the function as a TRIGGER on a table (e.g. mytable) | |
CREATE TRIGGER set_mytable_updated_at BEFORE UPDATE ON mytable FOR EACH ROW EXECUTE PROCEDURE set_updated_at() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment