drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.47k stars 486 forks source link

[BUG]: Circular reference causes type to become `any` #2476

Open jonkoops opened 3 weeks ago

jonkoops commented 3 weeks ago

What version of drizzle-orm are you using?

0.31.2

What version of drizzle-kit are you using?

0.22.6

Describe the Bug

When two tables reference each other in a circular manner the types of the tables cannot be inferred and instead become any.

Expected behavior

The tables have the correct type.

Environment & setup

import { integer, sqliteTable } from 'drizzle-orm/sqlite-core';

export const invitations = sqliteTable('invitations', {
  id: integer('id').primaryKey({ autoIncrement: true }).notNull(),
  primaryGuestId: integer('primary_guest_id').notNull().references(() => guests.id),
});

export const guests = sqliteTable('guests', {
  id: integer('id').primaryKey({ autoIncrement: true }).notNull(),
  invitationId: integer('invitation_id').notNull().references(() => invitations.id),
});
jonkoops commented 3 weeks ago

Note that this can be fixed with the solution mentioned by @dankochetov under https://github.com/drizzle-team/drizzle-orm/issues/435#issuecomment-1503542074, but this really feels like something that should work out of the box, and if not should be documented properly.