Sample Header Ad - 728x90

PostgreSQL: Auto update updated_at with custom column

8 votes
1 answer
35092 views
Before, I would join multiple tables into one and return result, but in many tables I had same column names. That's why I decided to to prefix column names with table name. But that broke the trigger I had to auto-update my updated_at column. This is the function I had: CREATE OR REPLACE FUNCTION update_updated_at() RETURNS TRIGGER AS $$ BEGIN NEW.updated_at = now(); RETURN NEW; END; $$ language 'plpgsql'; And I would add a trigger like: CREATE TRIGGER update_users_updated_at BEFORE UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE update_updated_at(); But now my column is named users_updated_at and this doesn't work. My question is is there any way that I can pass the column name to the trigger and update the passed column, or is there is any other way that I am not aware of?
Asked by SeemsIndie (183 rep)
Dec 20, 2016, 09:05 PM
Last activity: Aug 17, 2024, 05:11 AM