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?
No description
2 Replies
Artamiel
Artamiel8mo ago
You need to cast the reference using the ‘AnyPgColumn’ for example: ‘’’ table_2_ref: uuid("table_1_ref").references((): AnyPgColumn => table_2.id) ‘’’ more details: https://orm.drizzle.team/docs/indexes-constraints
Drizzle ORM - Indexes & Constraints
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Tobias
TobiasOP8mo ago
Thanks for the information!
Want results from more Discord servers?
Add your server