Issue when inserting

export const plan = pgTable("plans",{
id: uuid("id").primaryKey().notNull(),
userId: text("user_id").notNull().references(() => user.id),
budget: integer("budget").notNull(),
city: text("city").notNull(),
startDate:date("start_date").notNull(),
endDate:date("end_date").notNull(),
groupSize: integer("group_size").notNull(),
rating: integer("rating")
})

const planData = await db.insert(plan)
.values({
userId: "d",
budget: 222,
city: "WOW",
startDate: new Date(),
endDate: new Date(),
groupSize: 1
})
.returning()
export const plan = pgTable("plans",{
id: uuid("id").primaryKey().notNull(),
userId: text("user_id").notNull().references(() => user.id),
budget: integer("budget").notNull(),
city: text("city").notNull(),
startDate:date("start_date").notNull(),
endDate:date("end_date").notNull(),
groupSize: integer("group_size").notNull(),
rating: integer("rating")
})

const planData = await db.insert(plan)
.values({
userId: "d",
budget: 222,
city: "WOW",
startDate: new Date(),
endDate: new Date(),
groupSize: 1
})
.returning()
I get the error on userId
No overload matches this call.
Overload 2 of 2, '(values: { userId: string | SQL<unknown> | Placeholder<string, any>; id: string | SQL<unknown> | Placeholder<string, any>; budget: number | SQL<unknown> | Placeholder<string, any>; ... 4 more ...; rating?: number | ... 3 more ... | undefined; }[]): PgInsertBase<...>', gave the following error.
Object literal may only specify known properties, and 'userId' does not exist in type '{ userId: string | SQL<unknown> | Placeholder<string, any>; id: string | SQL<unknown> | Placeholder<string, any>; budget: number | SQL<unknown> | Placeholder<string, any>; ... 4 more ...; rating?: number | ... 3 more ... | undefined; }[]'.ts(2769)
No overload matches this call.
Overload 2 of 2, '(values: { userId: string | SQL<unknown> | Placeholder<string, any>; id: string | SQL<unknown> | Placeholder<string, any>; budget: number | SQL<unknown> | Placeholder<string, any>; ... 4 more ...; rating?: number | ... 3 more ... | undefined; }[]): PgInsertBase<...>', gave the following error.
Object literal may only specify known properties, and 'userId' does not exist in type '{ userId: string | SQL<unknown> | Placeholder<string, any>; id: string | SQL<unknown> | Placeholder<string, any>; budget: number | SQL<unknown> | Placeholder<string, any>; ... 4 more ...; rating?: number | ... 3 more ... | undefined; }[]'.ts(2769)
4 Replies
Angelelz
Angelelz11mo ago
You defined the id as notNull but didn't define a default value Drizzle is asking you to provide it
lettucebaran
lettucebaran7mo ago
@Russell what is your updated/fixed code? @Angelelz I don't understand why userId: "d", line in planData solves the issue because OP is passing a value to userId column. Could you elaborate please?
Angelelz
Angelelz7mo ago
Although the error is talking about userId, the problem is actually the id. In the table definition, id is not null but it doesn't have a default value, nor it autogenerates, hence a type error when you don't provide it in the insert Actually, rating is also required
lettucebaran
lettucebaran7mo ago
@Angelelz i posted a simplified version of this problem here (and default value is included), could we continue the discussion there please? https://discord.com/channels/1043890932593987624/1247493906627559495
Want results from more Discord servers?
Add your server