Auto update for updated_at
Any thoughts on how I could implement an auto updated field triggered on updates only on Postgres?
I see onUpdateNow() for MySql. Seems Postgres supports triggers for this. Can/should I use Postgres triggers via Drizzle?
If not, does it make sense to send the updated_at over the wire from the client?
9 Replies
hey @ken_peng did you get a solution for this? sorry i am new here, and am looking for this exact thing.
Could you add a custom type for this that just sets the insert value as
now()
/ new date()
?i think this is how most ORMs do this internally. when running an update query, they ensure that a
now()
is called for the updated_at
column because a user has given it a @updatedAt
or @UpdationTimestamp
annotation.
i was hoping this would be the case in drizzle as well, but having gone through the docs, seem to have found nothing for this.
so my plan for now is to manually update the updated_at
field everytime an UPDATE query is run, but would prefer if this was handled within the ORM
what do you think?You can add your own custom types (https://orm.drizzle.team/docs/custom-types), I think drizzle want to give you all the parts and not provide too much in an opiniated way 🙂
oh alright. i'll give it a look. thank you tho!!
Hey @dhruvaagrawal - How did you manage to get on with this? As I've actually hit the same problem that I'm about to start looking into.
hey, so I didn't actually solve the problem per se. i just started manually including the updated_at fields in every update query. i did this because i realised that for my system, the difference of milliseconds between the Date value generated by my codebase (which would happen if I did
updated_at = new Date()
) and the difference if the now()
function was run by the database or even the ORM, was not important, and the presence of the data was more than enough.Fair enough! That's good to know!
yea, but if you're able to solve this, please do let me know. i wouldn't mind switching over the ORM-controlled version of maintaining updated timestamps as they'd be a better design choice.
and would help reduce manual errors in case I forgot to include the
updated_at
field in any update query