DT
Drizzle Team•15mo ago
F0rce

executing an `update` statement does not use `.onUpdateNow()` defined in schema

Hello everyone 🙂 I have the following schema for my USERS table:
export const users = mysqlTable("USERS", {
id: varchar("ID", { length: 21 }).notNull().primaryKey(),
hostname: varchar("HOSTNAME", { length: 253 }).notNull(),
email: varchar("EMAIL", { length: 320 }).notNull(),
verified: boolean("VERIFIED").notNull().default(false),
createdAt: timestamp("CREATED_AT").notNull().defaultNow(),
updatedAt: timestamp("UPDATED_AT").onUpdateNow(),
});
export const users = mysqlTable("USERS", {
id: varchar("ID", { length: 21 }).notNull().primaryKey(),
hostname: varchar("HOSTNAME", { length: 253 }).notNull(),
email: varchar("EMAIL", { length: 320 }).notNull(),
verified: boolean("VERIFIED").notNull().default(false),
createdAt: timestamp("CREATED_AT").notNull().defaultNow(),
updatedAt: timestamp("UPDATED_AT").onUpdateNow(),
});
As I understand correctly, as soon as I use db.update(users)... the updatedAt should be automatically be set to new Data:
await db
.update(users)
.set({
...(putUser.email ? { email: putUser.email } : {}),
...(putUser.url ? { hostname: new URL(putUser.url).hostname } : {}),
})
.where(eq(users.id, user.id));
await db
.update(users)
.set({
...(putUser.email ? { email: putUser.email } : {}),
...(putUser.url ? { hostname: new URL(putUser.url).hostname } : {}),
})
.where(eq(users.id, user.id));
The email and hostname values are updated accordingly but the updatedAt column does not seem to be touched. Am I missunderstanding something here ? Thanks in advance, David
4 Replies
Andrii Sherman
Andrii Sherman•15mo ago
Do you have on update now() in your database directly?
F0rce
F0rce•15mo ago
I am using planetscale and as far as I know they dont support it so basically I just have to update it by myself
Andrii Sherman
Andrii Sherman•15mo ago
yes, if database has no support for it - it won't work currently in drizzle. Because everything defined in schema is full representation for database default/functions/constraints/etc.
F0rce
F0rce•15mo ago
ahhh good to know thanks a lot