Id being required in types when using insert and values

Hello! I' been following the tutorial and I'm having a issue with the following function:
export const addTodo = async (text: string) => {
await db.insert(todo).values({
text: text,
done: false,
});
revalidatePath("/");
};
export const addTodo = async (text: string) => {
await db.insert(todo).values({
text: text,
done: false,
});
revalidatePath("/");
};
I'm getting the following error:
No overload matches this call.
Overload 1 of 2, '(value: { id: number | SQL<unknown> | Placeholder<string, any>; text: string | SQL<unknown> | Placeholder<string, any>; done?: boolean | SQL<unknown> | Placeholder<string, any> | undefined; }): PgInsertBase<...>', gave the following error.
Argument of type '{ text: string; done: false; }' is not assignable to parameter of type '{ id: number | SQL<unknown> | Placeholder<string, any>; text: string | SQL<unknown> | Placeholder<string, any>; done?: boolean | SQL<unknown> | Placeholder<string, any> | undefined; }'.
Property 'id' is missing in type '{ text: string; done: false; }' but required in type '{ id: number | SQL<unknown> | Placeholder<string, any>; text: string | SQL<unknown> | Placeholder<string, any>; done?: boolean | SQL<unknown> | Placeholder<string, any> | undefined; }'.
Overload 2 of 2, '(values: { id: number | SQL<unknown> | Placeholder<string, any>; text: string | SQL<unknown> | Placeholder<string, any>; done?: boolean | SQL<unknown> | Placeholder<string, any> | undefined; }[]): PgInsertBase<...>', gave the following error.
Object literal may only specify known properties, and 'text' does not exist in type '{ id: number | SQL<unknown> | Placeholder<string, any>; text: string | SQL<unknown> | Placeholder<string, any>; done?: boolean | SQL<unknown> | Placeholder<string, any> | undefined; }[]'.ts(2769)
No overload matches this call.
Overload 1 of 2, '(value: { id: number | SQL<unknown> | Placeholder<string, any>; text: string | SQL<unknown> | Placeholder<string, any>; done?: boolean | SQL<unknown> | Placeholder<string, any> | undefined; }): PgInsertBase<...>', gave the following error.
Argument of type '{ text: string; done: false; }' is not assignable to parameter of type '{ id: number | SQL<unknown> | Placeholder<string, any>; text: string | SQL<unknown> | Placeholder<string, any>; done?: boolean | SQL<unknown> | Placeholder<string, any> | undefined; }'.
Property 'id' is missing in type '{ text: string; done: false; }' but required in type '{ id: number | SQL<unknown> | Placeholder<string, any>; text: string | SQL<unknown> | Placeholder<string, any>; done?: boolean | SQL<unknown> | Placeholder<string, any> | undefined; }'.
Overload 2 of 2, '(values: { id: number | SQL<unknown> | Placeholder<string, any>; text: string | SQL<unknown> | Placeholder<string, any>; done?: boolean | SQL<unknown> | Placeholder<string, any> | undefined; }[]): PgInsertBase<...>', gave the following error.
Object literal may only specify known properties, and 'text' does not exist in type '{ id: number | SQL<unknown> | Placeholder<string, any>; text: string | SQL<unknown> | Placeholder<string, any>; done?: boolean | SQL<unknown> | Placeholder<string, any> | undefined; }[]'.ts(2769)
and this is my schema:
import { integer, text, boolean, pgTable } from "drizzle-orm/pg-core";

export const todo = pgTable("todo_test", {
id: integer("id").primaryKey(),
text: text("text").notNull(),
done: boolean("done").default(false).notNull(),
});
import { integer, text, boolean, pgTable } from "drizzle-orm/pg-core";

export const todo = pgTable("todo_test", {
id: integer("id").primaryKey(),
text: text("text").notNull(),
done: boolean("done").default(false).notNull(),
});
What could be happening?
6 Replies
Sillvva
Sillvva4mo ago
Primary Key is not null by default, and you've not specified a default value.
chimelli
chimelli4mo ago
What should i do? Is primary key auto-increment? I searched through the docs and couldn't find anything related to.
Sillvva
Sillvva4mo ago
Drizzle ORM - PostgreSQL column types
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
chimelli
chimelli4mo ago
Thank you so much for taking time helping me. Unfortunately, this couldn't solve the typescript type issue I'm having. My issue is with primary key. I'm just following the tutorial on the drizzle page, and when i call insert(todo).values, typescript throws and erros telling me that id missing from the values.
Sillvva
Sillvva4mo ago
You are required to provide it, because integer primary keys do not have a default value out of the box.
chimelli
chimelli4mo ago
Oh, you're right. My fault. I was missing it. I tried and it worked now. Thank you again for your time.