inferSelect doesnt take into account defaults in schema

export const event = sqliteTable('event', {
id: integer('id').primaryKey({ autoIncrement: true }),
name: text('name').notNull(),
startTime: integer('start_time', { mode: 'timestamp' }).notNull(),
endTime: integer('end_time', { mode: 'timestamp' }).notNull(),
createdAt: integer('created_at', { mode: 'timestamp_ms' })
.notNull()
.default(sql`CURRENT_TIMESTAMP`),
updatedAt: integer('updated_at', { mode: 'timestamp_ms' })
.notNull()
.default(sql`CURRENT_TIMESTAMP`),
deletedAt: integer('deleted_at', { mode: 'timestamp_ms' }),
is_recurring: integer('is_recurring', { mode: 'boolean' }),
});
export type Event = typeof event.$inferSelect
export const event = sqliteTable('event', {
id: integer('id').primaryKey({ autoIncrement: true }),
name: text('name').notNull(),
startTime: integer('start_time', { mode: 'timestamp' }).notNull(),
endTime: integer('end_time', { mode: 'timestamp' }).notNull(),
createdAt: integer('created_at', { mode: 'timestamp_ms' })
.notNull()
.default(sql`CURRENT_TIMESTAMP`),
updatedAt: integer('updated_at', { mode: 'timestamp_ms' })
.notNull()
.default(sql`CURRENT_TIMESTAMP`),
deletedAt: integer('deleted_at', { mode: 'timestamp_ms' }),
is_recurring: integer('is_recurring', { mode: 'boolean' }),
});
export type Event = typeof event.$inferSelect
you can see createdAt has default
No description
12 Replies
Angelelz
Angelelz11mo ago
Which one of the columns you think has a problem?
Braveheart
BraveheartOP11mo ago
updatedAt: integer('updated_at', { mode: 'timestamp_ms' }) .notNull() .default(sqlCURRENT_TIMESTAMP), should be optional type
Angelelz
Angelelz11mo ago
You are probably looking for $inferInsert, that will give you the type for insert, where all columns that have default are optional
Braveheart
BraveheartOP11mo ago
ah ok thx
Braveheart
BraveheartOP11mo ago
its happy
No description
Ravi kiran
Ravi kiran11mo ago
hey guys im trying to insert start_dtime and end_dtime fields as integers. im trying the attached code. but i run into this error. Types of property 'start_dtime' are incompatible. Type 'number' is not assignable to type 'SQL<unknown> | Placeholder<string, any> | Date | null | undefined'. i have attached both my schema and the code. can someone help me with this.
No description
No description
Angelelz
Angelelz11mo ago
When you use { mode: 'timestamp_ms' }, that column now accepts a Date object, and drizzle will run .getTime() for you
Ravi kiran
Ravi kiran11mo ago
but it is an integer field right? how can i modify my code? any suggestions?
Angelelz
Angelelz11mo ago
Just remove .getTime() from start_date and end_date
Ravi kiran
Ravi kiran11mo ago
ok ill try and get back
Angelelz
Angelelz11mo ago
Just let it be a Date object
Ravi kiran
Ravi kiran11mo ago
yes it works. thank you.
Want results from more Discord servers?
Add your server