Open PurpleTape opened 5 months ago
For anyone Googling this (like me), a workaround is to change:
.$onUpdateFn(() => sql`now()`),
to
.$onUpdateFn(() => new Date()),
Related: https://github.com/drizzle-team/drizzle-orm/issues/2212
new Date()
uses server time :(
Here is my solution:
import { toZonedTime, format } from 'date-fns-tz'
function getCurrentTimeInTimeZoneISO(timeZone: string) {
const now = Date.now();
const zonedDate = toZonedTime(now, timeZone);
zonedDate.toISOString = () => format(zonedDate, "yyyy-MM-dd'T'HH:mm:ss'Z'", { timeZone })
return zonedDate
}
Use:
updatedAt: timestamp("updated_at").$onUpdate(() => getCurrentTimeInTimeZoneISO('America/Sao_Paulo'))
This issue doesn't happen when using node-postgres.
Here is my solution:
import { toZonedTime, format } from 'date-fns-tz' function getCurrentTimeInTimeZoneISO(timeZone: string) { const now = Date.now(); const zonedDate = toZonedTime(now, timeZone); zonedDate.toISOString = () => format(zonedDate, "yyyy-MM-dd'T'HH:mm:ss'Z'", { timeZone }) return zonedDate }
Use:
updatedAt: timestamp("updated_at").$onUpdate(() => getCurrentTimeInTimeZoneISO('America/Sao_Paulo'))
The time from this example is not the database time.
Here is my solution:
import { toZonedTime, format } from 'date-fns-tz' function getCurrentTimeInTimeZoneISO(timeZone: string) { const now = Date.now(); const zonedDate = toZonedTime(now, timeZone); zonedDate.toISOString = () => format(zonedDate, "yyyy-MM-dd'T'HH:mm:ss'Z'", { timeZone }) return zonedDate }
Use:
updatedAt: timestamp("updated_at").$onUpdate(() => getCurrentTimeInTimeZoneISO('America/Sao_Paulo'))
The time from this example is not the database time.
Yeah, but with that u can easily control the time. When changing the TZ of process.env, the "new Date" only returned the correct timezone date when using .toString(), but drizzle uses the Date toISOString method, so I wouldn't have the TZ date that I I wanted. So I took this approach with date-fns which solved the problem, thus managing to control the date my way. (OBS: this is my case)
Work-around that may help some people, can use now()
if you are happy to return a string
instead of a Date
.
updatedAt: timestamp('updated_at', {
withTimezone: true,
+ mode: 'string',
})
.defaultNow()
.notNull()
.$onUpdate(() => sql`now()`),
Drizzle ORM version 0.32.0, Postgres.js version 3.4.4
Work-around that may help some people, can use
now()
if you are happy to return astring
instead of aDate
.updatedAt: timestamp('updated_at', { withTimezone: true, + mode: 'string', }) .defaultNow() .notNull() .$onUpdate(() => sql`now()`),
Drizzle ORM version 0.32.0, Postgres.js version 3.4.4
Thanks!
What version of
drizzle-orm
are you using?0.30.10
What version of
drizzle-kit
are you using?0.21.4
Describe the Bug
An error occurs when trying to update a table with such a schema:
The occurrence of this error is due to the fact that when preparing a value for saving to the database, only the case is handled when the value is of type X https://github.com/drizzle-team/drizzle-orm/blob/a78eefe08e127922565486143e0150a718b27e8a/drizzle-orm/src/pg-core/columns/timestamp.ts#L65-L67
However, page DrizzleORM v0.30.5 release indicates that $onUpdate can take various values, including SQL
Expected behavior
No response
Environment & setup
No response