Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically add update triggers for postgres updated_at columns #2259

Open
rbalik opened this issue Jan 21, 2024 · 0 comments
Open

Automatically add update triggers for postgres updated_at columns #2259

rbalik opened this issue Jan 21, 2024 · 0 comments
Labels

Comments

@rbalik
Copy link

rbalik commented Jan 21, 2024

This may be a bit complex, but there is a "standardized" way of handling an updated_at column in postgres.

The idea is you create a function like this:

          CREATE FUNCTION update_updated_at_column() RETURNS trigger
            LANGUAGE plpgsql
            AS $$
          BEGIN
            NEW.updated_at = NOW();
            RETURN NEW;
          END;

This only has to be done once per database.

And then attach it to the column when it's created like this:

CREATE TRIGGER sometable_update_trigger BEFORE UPDATE ON sometable FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();

I'm doing this manually in migrations right now but it would be really cool if Phinx could do this automatically as part of the addTimestamps and addTimestampsWithTimezone calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants