© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•11mo ago•
2 replies
JROCBABY

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
});
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.
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!
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

Circular Reference Error when Using Self-Reference in Table Definition with Drizzle-ORM
Drizzle TeamDTDrizzle Team / help
2y ago
Circular Reference in Schema Relation
Drizzle TeamDTDrizzle Team / help
15mo ago
Circular reference in subquery
Drizzle TeamDTDrizzle Team / help
2y ago
sqlitetable circular reference
Drizzle TeamDTDrizzle Team / help
15mo ago