notNull() is still letting field be optional

I am performing the following for SQLite:
export const products = sqliteTable("products", {
id: integer("id").primaryKey({ autoIncrement: false }).notNull(),
name: text("name").notNull(),
description: text("description"),
});
export const products = sqliteTable("products", {
id: integer("id").primaryKey({ autoIncrement: false }).notNull(),
name: text("name").notNull(),
description: text("description"),
});
However, when I perform: export type NewProduct = typeof products.$inferInsert; It says that id is optional (this is what it shows when i hover over NewProduct)
type NewProduct = {
name: string;
id?: number | undefined; <---- SHOULD JUST BE NUMBER
description?: string | null | undefined;
}
type NewProduct = {
name: string;
id?: number | undefined; <---- SHOULD JUST BE NUMBER
description?: string | null | undefined;
}
How can I fix this?
4 Replies
Nick
Nick12mo ago
The problem seems to be that hasDefault is set to true; how can I force this to be false?
Nick
Nick12mo ago
No description
Nick
Nick12mo ago
To fix this, I have to do export type NewProduct = Omit<typeof products.$inferInsert, "id"> & { id: number }; But there should be a way to avoid this, no? Well this only solves the NewProduct but doesn't fix the insertProductSchema since that uses zod
Angelelz
Angelelz12mo ago
This is intended behavior $inferInsert is the type you would need to satisfy if you were inserting a row. Due to ID having a default, you can insert a row without providing an ID, hence number | undefined What you are looking for is $inferSelect
Want results from more Discord servers?
Add your server