11 Replies
not sure which driver/dialect you are using but for postgres it would be something like this
for updatedAt you can do this, but theres nothing implemented to automatically update the timestamp whenever changes to the row are made
these get compiled to the following btw
yeah unfortunately this is with sqlite 😦
i havent been able to find
timestamp_ms
on google so not sure what that isah okay, so it has to be a string basically right? was hoping to get the automatic conversion to Date drizzle provides
drizzle basically provides an automatic conversion to a Date object when using
sadly i am unable to test due to an issue but
maybe something like this
thank you! i wonder, does that return an int or a string? does SQL automatically cast strings when doing multiplications?
according to chatgpt
to my knowledge, as long as the generated sql command is valid, then sqlite should take care of the rest
the column in the database should be an integer, so drizzle should also return an integer
but i would really test it out since sqlite is not really made for things like these so you have to do some manual shenanigans to get the desired behavior
thanks for this!
was just having the same issue, trying to insert a millesec-timestamp into
integer("createdAt", {mode: "timestamp_ms" })
field.
Had to use the createdAt: integer('created_at', { mode: 'timestamp_ms' }).$default(() => new Date()),
which has a $default
function (instead of the native sql function).
It did work:
... but would have been nice to have a native solution.
I did a lot of trial and error trying to get the unixepoch('subsec')
to work, but it would just end up as a null
at the end. Maybe because it was a float
, and drizzle was expecting an int
.
This is the expression i used for the reference.
@IP @cvr @dandadan I was using IP's solution with a $default arrow function in my schema.ts returning
new Date()
, but the generated migration files didn't have SQL-defined DEFAULTs.
I found a solution that sets the SQL default and works with SQLite, returning the same timestamp format as new Date():