postgres naming trigger
Pattern [TBALE_NAME]_[FUNCITON NAME]_[WHEN][1 or more WHY][t]
When:
  • b - Before
  • a - After
  • i - Instead of
WHY:
  • i - Insert
  • u - Update
  • d - Delete
  • t - Truncate
t = 'trigger'
eg: my_table_update_ts_biut means it's a trigger on my_table, triggers update_ts before insert & update
Reasoning: Just by looking at the trigger name, you know which table, and when it is triggered
example
1
-- function to update updated_at column during insert or update
2
CREATE OR REPLACE FUNCTION update_timestamp()
3
RETURNS trigger AS
4
$
5
BEGIN
6
NEW.updated_at := NOW();
7
RETURN NEW;
8
END;
9
$ LANGUAGE 'plpgsql';
10
​
11
​
12
CREATE TRIGGER users_update_timestamp_biut
13
BEFORE INSERT OR UPDATE
14
ON users
15
FOR EACH ROW
16
EXECUTE PROCEDURE update_timestamp();
Copied!
Last modified 1mo ago
Copy link