DT
Drizzle Teamā€¢15mo ago
Jim

Type error inserting 'new Date()' into 'time' type schema.

Driving me up the wall this a bit. my schema
import { mysqlTable, serial, varchar, float, time } from 'drizzle-orm/mysql-core';

export const metalPrices = mysqlTable('metals', {
id: serial('id'),
metal: varchar('metal', { length: 5 }).primaryKey(),
value: float('value'),
time: time('time'),
});
import { mysqlTable, serial, varchar, float, time } from 'drizzle-orm/mysql-core';

export const metalPrices = mysqlTable('metals', {
id: serial('id'),
metal: varchar('metal', { length: 5 }).primaryKey(),
value: float('value'),
time: time('time'),
});
my insert
await db.insert(metalPrices).values({
metal: "FOO",
time: Date.now(), // type error when adding this line
});
await db.insert(metalPrices).values({
metal: "FOO",
time: Date.now(), // type error when adding this line
});
type error:
The expected type comes from property 'time' which is declared here on type '{ metal: string | SQL<unknown> | Placeholder<string, any>; value?: number | SQL<unknown> | Placeholder<string, any> | null | undefined; id?: number | SQL<unknown> | Placeholder<...> | undefined; time?: string | ... 3 more ... | undefined; }'
The expected type comes from property 'time' which is declared here on type '{ metal: string | SQL<unknown> | Placeholder<string, any>; value?: number | SQL<unknown> | Placeholder<string, any> | null | undefined; id?: number | SQL<unknown> | Placeholder<...> | undefined; time?: string | ... 3 more ... | undefined; }'
šŸ«  Any ideas?
5 Replies
Jim
JimOPā€¢15mo ago
Why does it want the input to be a string šŸ¤” 'timestamp' works ok (no type errors) but gives me '1970-01-20 14:30:39' which is why I was trying out other types ok the timecode I was getting needed to be *1000 and the insert using "on conflict" wasnt working properly, so it wasnt updating when I was debugging šŸ’€
ihernandez.
ihernandez.ā€¢15mo ago
Try: await db.insert(metalPrices).values({ metal: "FOO", time: sqlCURTIME(), });
Jim
JimOPā€¢15mo ago
Oh nice! thanks Thanks this is all working now!
Screw
Screwā€¢15mo ago
how can i compare a timestamp to a current time like gt(schema.timestamp, new Date())
ihernandez.
ihernandez.ā€¢15mo ago
You need convert the new Date() in a string format like that: 2023-08-31T20:11:36.159908, or you can calculate the current time in your database, for example: gt(schema.timestamp, sqlNOW())
Want results from more Discord servers?
Add your server