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?
D
dhruvaagrawal264d ago
hey @ken_peng did you get a solution for this? sorry i am new here, and am looking for this exact thing.
AL
Aidan Laycock264d ago
Could you add a custom type for this that just sets the insert value as now() / new date()?
D
dhruvaagrawal264d ago
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?
AL
Aidan Laycock264d ago
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 🙂
D
dhruvaagrawal264d ago
oh alright. i'll give it a look. thank you tho!!
AL
Aidan Laycock226d ago
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.
D
dhruvaagrawal226d ago
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.
AL
Aidan Laycock226d ago
Fair enough! That's good to know!
D
dhruvaagrawal226d ago
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
Want results from more Discord servers?
Add your server
More Posts
Deleted migrations, still can't create newI wanted to reset my entire database so I deleted my entire migrations folder and ran generate againHow to fetch related table rows in many_to_manyI have three tables : products, collections and collections_products. Product can belong to many colIs typescript slow for anyone else?Typescript is incredibly slow for anything related to drizzle, I have an m1 macbook pro with decent TypeError when adding subquery to `drizzle.insert.values`Hey all, I'm a bit new to drizzle so not sure if this is even possible or if I'm just doing somethinGet issue trying to run libsql/turso exampleon this project https://github.com/learn-anything/learn-anything/tree/main/db If I run `pnpm run stError on Vercel with drizzleI have everything setup and working locally with my new remix application which uses drizzle. HoweveAnyone have a project with Drizzle + Neon in Next.js?Anyone done a project with drizzle + neon in next.js that you can share?Infer Type from PgEnumHey, I'd like to infer a PgEnum to a TypeScript Type. So something like this: ``` const eventTypeEnnet::ERR_NAME_NOT_RESOLVEDHi, I was following the drizzle planetscale starter made by josh (https://github.com/joschan21/drizz@vercel/postgres & drizzle update?Looks like `const db = drizzle(sql)` does not work anymore with the latest version of drizzle & @vermany-many withHi. I have a schema with `users`, `roles` and `user_to_roles` tables. I also have a `usersRelations`help with drizzle implementation on streaming service overhaulHi Drizzle discord users, I recently started using typescript and next Js and would love to use drizdifferent `where` but same return type for relational queries?what's the best way to approach it when i have a fair few relational queries that have different wheOptional filteringI'm trying to figure out the right pattern to reproduce an SQL query where the user can optionally fAny way to do migrations with dataLooking for ways to insert data into the database as part of the migration. It appears it's unsupporFiltering findMany using related recordsI have a `restaurants` table that has a one-to-one relation with an `addresses` table. I want to pulDrizzle Kit Push & Generate gives weird errorI get the following error: ```ts { detail: undefined, id: '', location: [Object],re-formatting duplicated dataso i have a db with the schema shown below a `userPost` object consists of the following: ``` { Transforming const to the configured target environment ("es5") is not supported yetBelow is my schema file. `import { InferModel } from 'drizzle-orm' import { mysqlTable, serial, tenested relation queries```graphql userPost: { id, description, createdAt, expiresAt, user: { id, premium,