Why fields with default value is nullable

Consider this schema:
export const blogMetadata = pgTable('blog_metadata', {
id: varchar('id').primaryKey(),
view: integer('view').default(0),
});
export const blogMetadata = pgTable('blog_metadata', {
id: varchar('id').primaryKey(),
view: integer('view').default(0),
});
When I do a query:
db.select().from(blogMetadata).where(eq(blogMetadata.id, id))
db.select().from(blogMetadata).where(eq(blogMetadata.id, id))
The resultant type is:
{
id: string;
view: number | null
}
{
id: string;
view: number | null
}
Why is view nullable in this case? It should always be an integer due to the default value of 0, isn't it? Or am I missing something?
2 Replies
piechart
piechart10mo ago
You have to mark the column as NOT NULL. The default value is inserted if no value is provided, but you can still explicitly set a NULL value.
Zeraph
ZeraphOP10mo ago
Thanks a lot for the explanation
Want results from more Discord servers?
Add your server