TypeScript Error: Circular Reference in Drizzle ORM Schema

Hey everyone, I’m getting TypeScript errors in my Drizzle ORM schema due to a circular reference. Here’s the code:

typescript
Copy
Hey everyone, I’m getting TypeScript errors in my Drizzle ORM schema due to a circular reference. Here’s the code:

typescript
Copy
export const tableA = pgTable('tableA', {
  id: serial('id').primaryKey(),
  field1: text('field1').notNull(),
  field2: text('field2').notNull().unique(),
  field3: text('field3').notNull(),
  field4: text('field4').notNull(),
  field5: integer('field5').references(() => tableA.id), // Circular reference
});

Errors:

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

TS7024: Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.


What I’ve Tried:

Using InferModel to infer the table type.

Explicitly defining the table type with PgTableWithColumns.

How can I properly type this schema to resolve the circular reference errors? Any help would be appreciated!
Was this page helpful?