© 2026 Hedgehog Software, LLC

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

Two tables that references each other

How can I do two tables that references each other?

I have provided a minimal example to showcase what I am trying to achieve.
const table_1 = pgTable('table_1', {
    id: uuid('id').primaryKey().defaultRandom(),
    table_2_ref: uuid('table_1_ref').references(() => table_2.id)
});

const table_2 = pgTable('table_1', {
    id: uuid('id').primaryKey().defaultRandom(),
    table_1_ref: uuid('table_2_ref').references(() => table_1.id)
});
const table_1 = pgTable('table_1', {
    id: uuid('id').primaryKey().defaultRandom(),
    table_2_ref: uuid('table_1_ref').references(() => table_2.id)
});

const table_2 = pgTable('table_1', {
    id: uuid('id').primaryKey().defaultRandom(),
    table_1_ref: uuid('table_2_ref').references(() => table_1.id)
});


But when doing this I get a typescript error for both table_1 and table_2
'table_1' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

'table_2' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
'table_1' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

'table_2' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.


How can I solve this?
CleanShot_2024-05-23_at_12.06.402x.png
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

How do you define two tables that have foreign keys to each other? drizzle-kit introspect fails
Drizzle TeamDTDrizzle Team / help
2y ago
2 tables cannot reference each others columns?
Drizzle TeamDTDrizzle Team / help
2y ago
"Extend" tables by other tables
Drizzle TeamDTDrizzle Team / help
3y ago
Can't create relation between two tables
Drizzle TeamDTDrizzle Team / help
16mo ago