MySql NOW() in Drizzle

Hi! I might be struggling because this is hard to search for, but I'm wondering if it's possible to use mysql's NOW() function with Drizzle, rather than creating a new ts Date object and using that? Not that it matters much, but it would be good to keep everything in the db layer if possible. Apologies if I've missed this, I searched here, the reference docs site, and some READMEs on GitHub.
8 Replies
Andrii Sherman
Andrii Sherman17mo ago
Hi! Sure, it's possible. We have .defaultNow() on timestamps for MySQL -> column: timestamp("column_name").defaultNow() But we will deprecate it soon in a favour of CURRENT_TIMESTAMP which seems to work in the same way and also compitable in all environments so I would suggest to use this pattern after a column you want to have it
.default(sql`CURRENT_TIMESTAMP`)
.default(sql`CURRENT_TIMESTAMP`)
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_current-timestamp
Andrii Sherman
Andrii Sherman17mo ago
Andrii Sherman
Andrii Sherman17mo ago
For example PlanetScale(https://planetscale.com/) works not well with now() but workds good with CURRENT_TIMESTAMP
who
whoOP17mo ago
That's for setting the default in the table definition right? Rather than in for example a select or insert query? I guess I could use sqlCURRENT_TIMESTAMP) as the value in an insert into?
Andrii Sherman
Andrii Sherman17mo ago
yes, that's for setting the default in the table definition, which will make current column optional on inserts and then database will populate it for you
who
whoOP17mo ago
Yes - I mean if I want to use CURRENT_TIMESTAMP in a query or an insert I can do so with
sql`CURRENT_TIMESTAMP`
sql`CURRENT_TIMESTAMP`
right? (sorry, formatting isn't great)
Andrii Sherman
Andrii Sherman17mo ago
yes, you can use it not sure about inserts need to check it
who
whoOP17mo ago
Ok, I'll give it a go. Thanks! Just wanted to make sure I wasn't missing a dsl method for that.
Want results from more Discord servers?
Add your server