Auto update timestamp fields

How to auto update fields like updated_at?
S
stramel319d ago
you can add the .onUpdateNow() call to it. I'm doing something similar:
modifiedAt: timestamp('modified_at').defaultNow().onUpdateNow(),
modifiedAt: timestamp('modified_at').defaultNow().onUpdateNow(),
It may not be supported by some db providers such as planetscale
E
eatmoose308d ago
this doesn't exist
E
eatmoose306d ago
im on the latest orm version, still giving type error that it doesnt exist @.3819
J
Jim305d ago
What driver are you using?
AP
A. Perkamentus305d ago
Planetscale advices to update these values on app level, because if you use mysql functions like: "NOW()" or "CURRENT_TIMESTAMP()" they will use the mysql server time and timezone. If you want to use the timezone of your own app. set these values at app level. My Schema TS file:
const updatedAndCreatedAt = {
updatedAt: datetime("updatd_at").notNull(),
createdAt: datetime("created_at").notNull(),
};

export const users = table(
"users",
{
id: varchar("id", { length: 36 }).primaryKey(),
...all other fields,
...updatedAndCreatedAt,
}
);
const updatedAndCreatedAt = {
updatedAt: datetime("updatd_at").notNull(),
createdAt: datetime("created_at").notNull(),
};

export const users = table(
"users",
{
id: varchar("id", { length: 36 }).primaryKey(),
...all other fields,
...updatedAndCreatedAt,
}
);
When creating my record:
await db
.insert(schema.users)
.values({
id,
...al other fields,
createdAt: new Date(),
updatedAt: new Date(),
})
.execute();
await db
.insert(schema.users)
.values({
id,
...al other fields,
createdAt: new Date(),
updatedAt: new Date(),
})
.execute();
When updating:
await db
.update(schema.users)
.set({
...body,
updatedAt: new Date(),
})
.execute()
await db
.update(schema.users)
.set({
...body,
updatedAt: new Date(),
})
.execute()
E
eatmoose298d ago
postgres beep boop
D
dandadan298d ago
postgres does not have a feature like that sadly it can be achieved with triggers, but thats a database layer feature and not really recommended on the contrary, mysql seems to have some tools to automatically update timestamps
CREATE TABLE t1 (
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE t1 (
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
the previously mentioned function onUpdateNow() is probably available for mysql only in order for drizzle to support this for postgres they would have to add a module on top of every insert and pass the date there personally, i believe adding that into drizzle would sort of defeat the purpose of drizzle as a typesafe orm on top of drivers the feature does not exist in postgres and drizzle should only provide whatever is available for the database driver youre using
E
eatmoose298d ago
Ah ok. Yeah I prefer code first approach. I really don’t like adding triggers or functions I can’t see in my code
Want results from more Discord servers?
Add your server
More Posts
Relational query erroring on workers + D1Only able to get relation queries to work while running the worker in local mode. While running remInfer return type for relational query with paginationI have created a function that can paginate data for relational queries, problem is i cant figure oHow to implement triggers or db hooks ?Is there a way to know if sql statement got executed successfully or not ? I wanted to run a code whnoob help: INSERT with JOIN SQL to DrizzleFrontend developer dabbling with SQL for the first time in years. With the help of ChatGPT I have thOrderBy with dynamic queries dont work``` ....orderBy(sql`${products.xyz} ${order}`) ``` ``` error: syntax error at or near "$2" ``` ordeIs there a way to limit update to just 1 document?I know there will be multiple documents matching this where query, but I only want to update 1 of thHow to get InferModel to work with Relation model?Is there a way to get the infer type for a relation model? eg: type User = InferModel<typeof users,MySQL column type: "Generated"?Does drizzle support defining generated columns? https://planetscale.com/courses/mysql-for-developeAnyone else having a weird issue with drizzle-kit push:mysql?I spun up a new project using Planetscale and I got a bizarre __vtschema error (see: https://github.Performance questionsI'm using Drizzle ORM with Planetscale DatabaseJS driver and my application has spots where I want tCan drizzle generate an initial schema from an existing db?I have a db that wasn't built using drizzle, but I'm interesting in moving over to it. Do I need toPrepared StementsI was looking into using AWS RDS Proxy with Prisma and ran into this https://www.prisma.io/docs/guiReact Native SupportHi all New to drizzle ORM, does it support React Native? Thanksupdate multiple rowsHow can I update values in multiple rows at once?Running `drizzle-kit introspect:pg` returns "client password must be a string"I'm attempting to move from objection.js+knex.js over to Drizzle and I'm running `drizzle-kit introsnoob help : transaction not acid``` return await this.drizzle.db.transaction(async (trx) => { const u = await trx .seExplicit inferred types```ts async loginWithGoogle(user: NonNullable<Request['user']>) { const existingUser = await thiMany-to-Many where in far tableI'm trying to `findMany()` Pokemon(s) by a type name ``` pokemons pokemonsToType Cannot call onConflictDoNothing() or on onConflctDoUpdate() on selectHello guys, this is probably right in front of me, but when I insert at the end of values I cannot fTop-level await is not available in the configured target environment ("chrome87", "edge88", "es2020Hey I just upgraded to the v^0.26.2 and I get the following error: ``` Top-level await is not avai