defaultFn not nullable

I have this schema:
export const userTableSchema = pgTable(
"user",
{
id: text("id")
.$defaultFn(() => createId())
.primaryKey()
.notNull(),
createdAt: timestamp("created_at", {
withTimezone: true,
mode: "date",
}).notNull(),
confirmedEmail: boolean("confirmed_email")
.notNull()
.$default(() => false),
confirmedPhone: boolean("confirmed_phone")
.notNull()
.$default(() => false),
name: varchar("name", { length: 255 }).notNull(),
email: varchar("email", { length: 255 }).notNull(),
},
(table) => {
return {
emailIdx: uniqueIndex("users_email_idx").on(table.email),
}
}
)
export const userTableSchema = pgTable(
"user",
{
id: text("id")
.$defaultFn(() => createId())
.primaryKey()
.notNull(),
createdAt: timestamp("created_at", {
withTimezone: true,
mode: "date",
}).notNull(),
confirmedEmail: boolean("confirmed_email")
.notNull()
.$default(() => false),
confirmedPhone: boolean("confirmed_phone")
.notNull()
.$default(() => false),
name: varchar("name", { length: 255 }).notNull(),
email: varchar("email", { length: 255 }).notNull(),
},
(table) => {
return {
emailIdx: uniqueIndex("users_email_idx").on(table.email),
}
}
)
when I set .$defaultFn(() => createId()) for the Id property it marks it as Optional: Is that a way to change that behavior and make it not nullable?
No description
1 Reply
rphlmr ⚡
rphlmr ⚡5mo ago
It is nullable for the schema because you have $defaultFn. So, id is not mandatory since a default value will be set.