Not null must have default value as well?

Noob question. Does a "not null" value have to have a default value? For some reason it seems like the case. This insert snippet is not working. and giving me a Object literal may only specify known properties, and email does not exist in type and Object literal may only specify known properties, and first_name does not exist in type When I comment out phone and email from my schema/make them both nullable/add a default value it doesn't give me this error.
async signup(dto: AuthDto) {
const hash = await argon.hash(dto.password);
const user = await this.dbService.db
.insert(schema.users)
.values({
email: dto.email,
phone: dto.phone
password_hash: hash,
first_name: 'test',
last_name: 'user',
role: schema.userRoleEnum.enumValues[0],
created_at: new Date(),
})
.returning();
}
async signup(dto: AuthDto) {
const hash = await argon.hash(dto.password);
const user = await this.dbService.db
.insert(schema.users)
.values({
email: dto.email,
phone: dto.phone
password_hash: hash,
first_name: 'test',
last_name: 'user',
role: schema.userRoleEnum.enumValues[0],
created_at: new Date(),
})
.returning();
}
schema.ts
import {
pgTable,
text,
uuid,
boolean,
timestamp,
pgEnum,
varchar,
} from 'drizzle-orm/pg-core';

export const userRoleEnum = pgEnum('user_role', [
'user',
'admin',
'branch_manager',
'restaurant_owner',
]);

export const users = pgTable('users', {
id: uuid('id').notNull().defaultRandom().primaryKey(),
email: varchar('email', { length: 255 }).unique(),
phone: varchar('phone', { length: 20 }).notNull().unique(),
first_name: varchar('first_name', { length: 50 }),
last_name: varchar('last_name', { length: 50 }),
email_verified: boolean('email_verified').default(false),
phone_verified: boolean('phone_verified').default(false),
created_at: timestamp('created_at').notNull().defaultNow(),
role: userRoleEnum('role').notNull().default('user'),
password_hash: text('password_hash').notNull(),
});
import {
pgTable,
text,
uuid,
boolean,
timestamp,
pgEnum,
varchar,
} from 'drizzle-orm/pg-core';

export const userRoleEnum = pgEnum('user_role', [
'user',
'admin',
'branch_manager',
'restaurant_owner',
]);

export const users = pgTable('users', {
id: uuid('id').notNull().defaultRandom().primaryKey(),
email: varchar('email', { length: 255 }).unique(),
phone: varchar('phone', { length: 20 }).notNull().unique(),
first_name: varchar('first_name', { length: 50 }),
last_name: varchar('last_name', { length: 50 }),
email_verified: boolean('email_verified').default(false),
phone_verified: boolean('phone_verified').default(false),
created_at: timestamp('created_at').notNull().defaultNow(),
role: userRoleEnum('role').notNull().default('user'),
password_hash: text('password_hash').notNull(),
});
10 Replies
rphlmr ⚡
rphlmr ⚡5mo ago
Not null requires a value (by default your column is nullable). Without a .default (or .$default, .$defaultFn), you have to provide a value. It makes it require
yehia
yehiaOP5mo ago
Yeah I'm getting this error even when I pass values in on insert for email/password_hash so that's what's confusing. I don't want email/password_hash to have default values, I want them to be not null and I pass their values in on insert
rphlmr ⚡
rphlmr ⚡5mo ago
https://drizzle.run/r212ev8wolhh9fg4nn1esvr6 I set email to be not null (required on insert) and have no error.
rphlmr ⚡
rphlmr ⚡5mo ago
feel free to tell me if I misunderstood something
yehia
yehiaOP5mo ago
You're totally right and this is expected behavior. It may be something with my particular setup with NestJS. I'll see if there's something there
Deepak Patil
Deepak Patil5mo ago
Have you found any solution, facing the same issue after upgrading drizzle orm to latest
yehia
yehiaOP5mo ago
Drizzle ORM - Goodies
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
yehia
yehiaOP5mo ago
After having these types I use them like this
await db.insert(schema.users).values({ name: 'Andrew' } as schema.InsertUsers);
await db.insert(schema.users).values({ name: 'Andrew' } as schema.InsertUsers);
Deepak Patil
Deepak Patil5mo ago
For update query? facing the same as inferInsert wont work on update statement
yehia
yehiaOP5mo ago
Why not Can you show me the error
Want results from more Discord servers?
Add your server