© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
1 reply
DoronT

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
categoriesTable
as
any
any

It's only when I remove
.references(() => categoriesTable.id, { onDelete: 'set null' })
.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?
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

SQLite self referencing table
Drizzle TeamDTDrizzle Team / help
12mo ago
Self referencing table query
Drizzle TeamDTDrizzle Team / help
2y ago
Self referencing
Drizzle TeamDTDrizzle Team / help
3y ago
Self referencing composite primary key table
Drizzle TeamDTDrizzle Team / help
14mo ago