`id` type is `undefined` when using `serial("id").primaryKey()`

import { sql } from "drizzle-orm"
import { boolean, pgTable, serial, uuid, varchar } from "drizzle-orm/pg-core"
import { createInsertSchema } from "drizzle-zod"
import type { z } from "zod"

export const users = pgTable("users", {
id: serial("id").primaryKey(),
})

export const insertUserSchema = createInsertSchema(users)
export type User = z.infer<typeof insertUserSchema>
import { sql } from "drizzle-orm"
import { boolean, pgTable, serial, uuid, varchar } from "drizzle-orm/pg-core"
import { createInsertSchema } from "drizzle-zod"
import type { z } from "zod"

export const users = pgTable("users", {
id: serial("id").primaryKey(),
})

export const insertUserSchema = createInsertSchema(users)
export type User = z.infer<typeof insertUserSchema>
Part of the issue is I always have to validate id exists:
"use server"

import { postAuthenticateClient } from "@/lib/telegra/"
import { type User, users } from "@/drizzle/schema"
import { db } from "@/drizzle/db"
import { eq } from "drizzle-orm"

interface PutSetPatientSSOKeyParams {
id: User["id"]
}

export default async function putSetPatientSSOKey({
id,
}: PutSetPatientSSOKeyParams) {
// ...code
await db
.update(users)
.set({
telegra_sso_key: telegraSSOKey,
})
.where(eq(users.id, id))

// ...code
}
"use server"

import { postAuthenticateClient } from "@/lib/telegra/"
import { type User, users } from "@/drizzle/schema"
import { db } from "@/drizzle/db"
import { eq } from "drizzle-orm"

interface PutSetPatientSSOKeyParams {
id: User["id"]
}

export default async function putSetPatientSSOKey({
id,
}: PutSetPatientSSOKeyParams) {
// ...code
await db
.update(users)
.set({
telegra_sso_key: telegraSSOKey,
})
.where(eq(users.id, id))

// ...code
}
No description
3 Replies
David L. Bowman
David L. BowmanOP•3mo ago
id can be undefined because it's created as a serial, but... i'm using .update so it obviously exists. what's the best way to do this? I can do id as number but that's bad practice 😦 I could do this, maybe this is best?
if (id) {
await db
.update(users)
.set({
telegra_sso_key: telegraSSOKey,
})
.where(eq(users.id, id))
}
if (id) {
await db
.update(users)
.set({
telegra_sso_key: telegraSSOKey,
})
.where(eq(users.id, id))
}
Angelelz
Angelelz•3mo ago
Drizzle ORM - Goodies
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Angelelz
Angelelz•3mo ago
In particular:
export type User = typeof users.$inferSelect;
export type User = typeof users.$inferSelect;
Want results from more Discord servers?
Add your server