DT
Drizzle Teamā€¢10mo 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
Jimā€¢10mo 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.ā€¢10mo ago
Try: await db.insert(metalPrices).values({ metal: "FOO", time: sqlCURTIME(), });
Jim
Jimā€¢10mo ago
Oh nice! thanks Thanks this is all working now!
Screw
Screwā€¢10mo ago
how can i compare a timestamp to a current time like gt(schema.timestamp, new Date())
ihernandez.
ihernandez.ā€¢10mo 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
More Posts
drizzle asks for <table> `id` when doing insertI guess the ID should be autogenerated by the database itself, but not sure why it's asking me to paData factoriesIs there a good way to add a data factory to Drizzle, so that I can add new users quickly for testinPrepared Statement doesn't exist``` "message": "db error: ERROR: prepared statement \"s114\" does not exist\n\nCaused by:\n ERRHelp creating many to many (user has many followers, user has many followees)is this the right solution ?! my first attempts was putting 2 fields on the users followers, followeInconsistent transactions with Drizzle/PlanetscaleI'm having issues with updating multiple records in parallel using `Promise.all` inside a transactioERROR: prepared statement "s9656" does not existNot sure why I'm getting this because I have no prepared statement. I thought it might have been aPostgresError: unrecognized configuration parameter "schema" when creating client for postgres-jsGetting the error when I add schema to the call drizzle(client, {schema}) using version "drizzle-orDoes throwing a regular javascript error inside a transaction, roll back automatically?Curious if I need to actually call tx.rollback()? Or if we reach a regular error, or lets say a PostDrizzle join in a subquery results in ambiguous columnsThis fails with `id ambiguous` error, since the `orderedQuestions.survey_question.id` reference in tSchema Definition Performance / Best PracticeIs it considered bad practice to have one massive client / schema definition for my whole project th