DT
Drizzle Team•3w ago
Alve

Updating a date with the node-postgres adapter

I am trying to update a date value like this:
await db
.update(schema.order)
.set({ completedDate: new Date() }) // same error with sql`now()`
.where(eq(schema.order.id, order.id));
await db
.update(schema.order)
.set({ completedDate: new Date() }) // same error with sql`now()`
.where(eq(schema.order.id, order.id));
But I get an error value.toISOString is not a function, and searching that up, I came across this: https://github.com/drizzle-team/drizzle-orm/issues/2388 In the schema it is defined like this: completedDate: timestamp({ withTimezone: true }), I am not really sure how to solve it, I'd prefer to solve it without using type: 'string', because there's other code depending on it being a Date object returned.. Am I doing something wrong or is it just broken?
7 Replies
TOSL
TOSL•3w ago
All the db drivers by default always returns strings for dates. You have to set the mode on you column to tell Drizzle how to handle it from there.
Alve
AlveOP•3w ago
even when setting mode: "date" I get the exact same error
TOSL
TOSL•3w ago
Assuming you haven't had it set from the very beginning and you've seeded your db with some things already, maybe try dropping your db.
francis
francis•3w ago
dropping the db won't help here, this isn't a sql datatype problem check with a select to see what actual type you get out the other end, to make sure it's operating in date mode properly
Alve
AlveOP•3w ago
Yeah it pruned my local db when I changed it so it should be fine I get out a date if I'm querying it, and that date object has toISOString. This happens somewhere internally in drizzle
francis
francis•3w ago
if you pass in the result of the query to an update, what happens?
Alve
AlveOP•3w ago
that works fine apparently but setting completedDate: queryResult.completedDate fails I think I figured it out, I have a updatedAt that I forgot, that is exactly like what's in the issue I linked on the table 🙃 If I remove the $onUpdate it works, so I guess the date works, but I need to provide the old date from updatedAt...

Did you find this page helpful?