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

Find a good solution for update_at on the postgresql backend #135

Closed
joseferben opened this issue Jun 5, 2020 · 5 comments
Closed

Find a good solution for update_at on the postgresql backend #135

joseferben opened this issue Jun 5, 2020 · 5 comments

Comments

@joseferben
Copy link
Contributor

No description provided.

@tmattio
Copy link

tmattio commented Jun 17, 2020

Hey @Jerben 🙂

I was looking into Sihl (really nice project!!) and stumbled upon this issue, so I thought I'd share this piece of code that I use for updated_at fields:

CREATE OR REPLACE FUNCTION "public"."set_current_timestamp_updated_at"()
RETURNS TRIGGER AS $$
DECLARE
  _new record;
BEGIN
  _new := NEW;
  _new."updated_at" = now();
  RETURN _new;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER "set_public_users_updated_at"
BEFORE UPDATE ON "public"."users"
FOR EACH ROW
EXECUTE PROCEDURE "public"."set_current_timestamp_updated_at"();

This will set the value of updated_at to now() every time the row is modified.

Not sure this is what you meant, but I hope it helps 😄

@joseferben
Copy link
Contributor Author

Hey @tmattio, thanks a lot for the kind words and the nice input! 🙂

Once the MariaDB driver supports non-prepared statements, this will be most likely the solution.
paurkedal/ocaml-caqti#42

@tmattio
Copy link

tmattio commented Jun 17, 2020

Good to know, thanks for the link!

@joseferben
Copy link
Contributor Author

@tmattio I just realized this is only an issue for Postgres 🙂 Last time I checked the issue for the MariaDB driver doesn't really exist for the Postgres driver, so this works! 👍

@joseferben
Copy link
Contributor Author

For now we are doing that on application level so it is repo independent.

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

No branches or pull requests

2 participants