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.57k stars 490 forks source link

[BUG]: Nested partial select type is not recursive #954

Open relsunkaev opened 11 months ago

relsunkaev commented 11 months ago

What version of drizzle-orm are you using?

0.27.2

Describe the Bug

Not 100% sure if this is a bug or a feature request but the following query works and produces the desired result

const result = await db
  .select({
    id: table.id,
    levelOne: {
      id: levelOne.id,
      levelTwo: {
        id: levelTwo.id,
      }
    },
  })
  .from(table)
  .leftJoin(
    levelOne,
    eq(levelOne.tableId, table.id),
  )
  .leftJoin(
    levelTwo,
    eq(levelTwo.levelOneId, levelOne.id),
  );

but the types give the following error

Type '{ id: PgInteger<{ tableName: "levelTwo"; name: string; data: number; driverParam: string | number; hasDefault: false; notNull: false; }>; levelOneId:  PgInteger<...>; }' is not assignable to type 'AnyPgColumn<{}> | SQL<unknown> | Aliased<unknown>'.
  Type '{ id: PgInteger<{ tableName: "levelTwo"; name: string; data: number; driverParam: string | number; hasDefault: false; notNull: false; }>; levelOneId:  PgInteger<...>; }' is missing the following properties from type 'PgColumn<PgColumnHKT, Required<{ name: string; data: unknown; driverParam: unknown; notNull: boolean; hasDefault: boolean; tableName: string; }>, {}, {}>': table, _, primary, notNull, and 9 more.

Expected behavior

No typescript error and correct return type inference.

Environment & setup

No response

philwinder commented 11 months ago

I think this is the same as https://github.com/drizzle-team/drizzle-orm/issues/531

relsunkaev commented 10 months ago

I think this is the same as https://github.com/drizzle-team/drizzle-orm/issues/531

That issue seems unrelated, at least to me.

Angelelz commented 7 months ago

Can you confirm this is still an issue?

relsunkaev commented 7 months ago

Yep, still an issue as of 0.29.0

milan-codes commented 5 months ago

Ran into a somewhat similar issue on 0.29.3, @Angelelz. Made a simple repo that reproduces it.

Angelelz commented 5 months ago

I just took a look at the codebase and it seems to me that the select object that you define is not designed to be recursive and you are only allowed to one level of nesting. If anything, this should be a feature request, not a bug. The type error is correct in this case.