© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago
DiamondDragon

Losing custom $types in pg jsonb using drizzle-zod

Not sure if this is an expected limitation of zod here
export const records = pgTable('records', {
    id: uuid('id')
        .primaryKey()
        .$defaultFn(() => uuidv7()),
    tableId: uuid('table_id')
        .references(() => tables.id, { onDelete: 'cascade' })
        .notNull(),
    organizationId: uuid('organization_id')
        .references(() => organizations.id, { onDelete: 'cascade' })
        .notNull(),
    recordByFieldId: jsonb('record_by_field_id').$type<RecordData>(),

})
export const records = pgTable('records', {
    id: uuid('id')
        .primaryKey()
        .$defaultFn(() => uuidv7()),
    tableId: uuid('table_id')
        .references(() => tables.id, { onDelete: 'cascade' })
        .notNull(),
    organizationId: uuid('organization_id')
        .references(() => organizations.id, { onDelete: 'cascade' })
        .notNull(),
    recordByFieldId: jsonb('record_by_field_id').$type<RecordData>(),

})


produces using $inferInsert
type Record = {
    organizationId: string;
    tableId: string;
    id?: string;
    recordByFieldId?: RecordData | null;
    createdAt?: Date;
    updatedAt?: Date | null;
}
type Record = {
    organizationId: string;
    tableId: string;
    id?: string;
    recordByFieldId?: RecordData | null;
    createdAt?: Date;
    updatedAt?: Date | null;
}


But when using drizzle zod, i lose RecordData
export const insertRecordSchema = createInsertSchema(records)
export type InsertRecord = z.infer<typeof insertRecordSchema>

// results in
type InsertRecord = {
    organizationId: string;
    tableId: string;
    id?: string;
    recordByFieldId?: any;
    createdAt?: Date;
    updatedAt?: Date | null;
}
export const insertRecordSchema = createInsertSchema(records)
export type InsertRecord = z.infer<typeof insertRecordSchema>

// results in
type InsertRecord = {
    organizationId: string;
    tableId: string;
    id?: string;
    recordByFieldId?: any;
    createdAt?: Date;
    updatedAt?: Date | null;
}
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

drizzle-zod with custom types
Drizzle TeamDTDrizzle Team / help
3y ago
Drizzle-zod createInsertSchema types
Drizzle TeamDTDrizzle Team / help
3y ago
drizzle-zod not infering jsonb type properly
Drizzle TeamDTDrizzle Team / help
2y ago
help with drizzle-zod with jsonb column
Drizzle TeamDTDrizzle Team / help
3y ago