Self referencing column breaks table type in typescript

I have the following table:
export const categoriesTable = pgTable(
'categories',
{
id: uuid('id').primaryKey(),
name: varchar('name', { length: 50 }).notNull(),
description: text('description'),
parentId: uuid('parent_id').references(() => categoriesTable.id, { onDelete: 'set null' }),
createdAt: timestamp('created_at').defaultNow()
},
(table) => ({
nameIdx: uniqueIndex('name_idx').on(table.name)
})
)
export const categoriesTable = pgTable(
'categories',
{
id: uuid('id').primaryKey(),
name: varchar('name', { length: 50 }).notNull(),
description: text('description'),
parentId: uuid('parent_id').references(() => categoriesTable.id, { onDelete: 'set null' }),
createdAt: timestamp('created_at').defaultNow()
},
(table) => ({
nameIdx: uniqueIndex('name_idx').on(table.name)
})
)
However typescript infers the type of categoriesTable as any It's only when I remove .references(() => categoriesTable.id, { onDelete: 'set null' }) that typescript infers the type correctly If so, how can I still create a self reference in this case?
1 Reply
Huge Letters
Huge Letters9mo ago
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Want results from more Discord servers?
Add your server