drizzle asks for <table> `id` when doing insert

I guess the ID should be autogenerated by the database itself, but not sure why it's asking me to pass it as a value when doing
// This throwns TS error saying that ID is missing
const data = await db.insert(workspaces).values([
{
ownerId: session.user.id,
company: parsed.data.company,
name: parsed.data.name,
},
]);
// This throwns TS error saying that ID is missing
const data = await db.insert(workspaces).values([
{
ownerId: session.user.id,
company: parsed.data.company,
name: parsed.data.name,
},
]);
using mysql, @planetscale/database my schemas:
export const workspaces = mysqlTable("workspaces", {
id: varchar("id", { length: 255 }).primaryKey(),
name: varchar("name", { length: 255 }).notNull(),
company: varchar("company", { length: 255 }).notNull(),
ownerId: varchar("ownerId", { length: 255 }).notNull(),
createdAt: timestamp("createdAt", { mode: "date" }).notNull().defaultNow(),
});

export const workspacesPickedSchema = createInsertSchema(workspaces)
.pick({
name: true,
company: true,
})
.required();
export const workspaces = mysqlTable("workspaces", {
id: varchar("id", { length: 255 }).primaryKey(),
name: varchar("name", { length: 255 }).notNull(),
company: varchar("company", { length: 255 }).notNull(),
ownerId: varchar("ownerId", { length: 255 }).notNull(),
createdAt: timestamp("createdAt", { mode: "date" }).notNull().defaultNow(),
});

export const workspacesPickedSchema = createInsertSchema(workspaces)
.pick({
name: true,
company: true,
})
.required();
4 Replies
Luxaritas
Luxaritas16mo ago
You never specify in your schema that the id should be autogenerated - as-is, it’s just a normal string column
Liltripple_reid
Liltripple_reidOP16mo ago
isn't it what .primaryKey() does?
ihernandez.
ihernandez.16mo ago
with .primaryKey() you only indicate that this column is the key of the record in the table, you must indicate separately that it is an AUTO_INCREMENT data, or you can use id: uuid('id').defaultRandom().primaryKey().
Liltripple_reid
Liltripple_reidOP16mo ago
I get it now, thanks a lot
Want results from more Discord servers?
Add your server