drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: Left Join PgColumn Incompatible After the Latest Version

Open yWilliamLima opened this issue 1 year ago • 15 comments

Report hasn't been filed before.

  • [x] I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.43.x

What version of drizzle-kit are you using?

0.31.0

Other packages

No response

Describe the Bug

The issue started occurring from version 0.43.x onwards; it was working normally before that. It occurs when passing the user table in a join to service!

Code:

import { db } from '@repo/drizzle/index'
import { eq } from 'drizzle-orm'
import * as t from 'drizzle-orm/pg-core'

export const user = t.pgTable('users', {
  id: t.integer().primaryKey().generatedAlwaysAsIdentity(),
  name: t.varchar().notNull(),
})

export const service = t.pgTable('services', {
  id: t.integer().primaryKey().generatedAlwaysAsIdentity(),
  name: t.varchar().notNull(),
  authorId: t
    .integer('author_id')
    .references(() => user.id, {
      onUpdate: 'cascade',
    })
    .notNull(),
})

export const test = async () => {
  await db.select().from(service).leftJoin(user, eq(service.authorId, user.id))
}

Error:

Argument of type 'PgTableWithColumns<{ name: "users"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "users"; dataType: "number"; columnType: "PgInteger"; data: number; driverParam: string | number; notNull: true; hasDefault: true; ... 6 more ...; generated: undefined; }, {}, {}>; name: PgColumn<...>; }; dialect:...' is not assignable to parameter of type 'SQL<unknown> | Subquery<string, Record<string, unknown>> | PgViewBase<string, boolean, ColumnsSelection> | PgTable<TableConfig>'.
  Type 'PgTableWithColumns<{ name: "users"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "users"; dataType: "number"; columnType: "PgInteger"; data: number; driverParam: string | number; notNull: true; hasDefault: true; ... 6 more ...; generated: undefined; }, {}, {}>; name: PgColumn<...>; }; dialect:...' is not assignable to type 'PgTable<TableConfig>'.
    The types of '_.config.columns' are incompatible between these types.
      Type '{ id: PgColumn<{ name: "id"; tableName: "users"; dataType: "number"; columnType: "PgInteger"; data: number; driverParam: string | number; notNull: true; hasDefault: true; isPrimaryKey: true; isAutoincrement: false; ... 4 more ...; generated: undefined; }, {}, {}>; name: PgColumn<...>; }' is not assignable to type 'Record<string, PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>>'.
        Property 'id' is incompatible with index signature.
          Type 'PgColumn<{ name: "id"; tableName: "users"; dataType: "number"; columnType: "PgInteger"; data: number; driverParam: string | number; notNull: true; hasDefault: true; isPrimaryKey: true; isAutoincrement: false; ... 4 more ...; generated: undefined; }, {}, {}>' is not assignable to type 'PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>'.
            The types of 'table._.config.columns' are incompatible between these types.
              Type 'Record<string, import("/home/william/github/turborepo/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]_5b710c8e0a8bb205446dd39d33d33cc0/node_modules/drizzle-orm/pg-core/columns/common").PgColumn<import("/home/william/github/turborepo/node_modules/.pnpm/drizzl...' is not assignable to type 'Record<string, import("/home/william/github/turborepo/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]_5b710c8e0a8bb205446dd39d33d33cc0/node_modules/drizzle-orm/pg-core/columns/common", { with: { "resolution-mode": "import" } }).PgColumn<import("/home/william...'.
                'string' index signatures are incompatible.
                  Type 'import("/home/william/github/turborepo/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]_5b710c8e0a8bb205446dd39d33d33cc0/node_modules/drizzle-orm/pg-core/columns/common").PgColumn<import("/home/william/github/turborepo/node_modules/.pnpm/[email protected]_@p...' is not assignable to type 'import("/home/william/github/turborepo/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]_5b710c8e0a8bb205446dd39d33d33cc0/node_modules/drizzle-orm/pg-core/columns/common", { with: { "resolution-mode": "import" } }).PgColumn<import("/home/william/github/turbore...'.
                    Property 'config' is protected but type 'Column<T, TRuntimeConfig, TTypeConfig>' is not a class derived from 'Column<T, TRuntimeConfig, TTypeConfig>'.

Details: ts-error-translator

yWilliamLima avatar Apr 25 '25 16:04 yWilliamLima

I’m running into the same problem after upgrading to [email protected].

Doing any kind of join results in a TypeScript type error that didn’t happen before.

lgabeskiria avatar Apr 28 '25 06:04 lgabeskiria

I have the same issue with version 0.43.1. In version 0.42.0, this code worked well:

const clientsWithoutCreds = await db
    .select()
    .from(clients)
    .leftJoin(creds, and(eq(clients.id, creds.clientId), eq(creds.key, "creds")))
    .where(isNull(creds.key));
for (let c of clientsWithoutCreds) {
    await db.delete(clients).where(eq(clients.id, c.clients.id));
    deleted++;
}

After upgrading, the type of c.clients.id is strange and not compatible with clients.id on eq()

qoqn avatar Apr 29 '25 03:04 qoqn

Same issue on my side: cannot upgrade to 0.43.x

robinshin avatar May 06 '25 07:05 robinshin

Same issue on my side: cannot upgrade to 0.43.x

PNGdS avatar May 06 '25 11:05 PNGdS

I get the same issue using MySQL and a monorepo. There seems to be typing changes to join https://github.com/drizzle-team/drizzle-orm/compare/0.42.0...0.43.1#diff-941bd29eda261190fe8bfd11132a27545b6b5fbdceb3ca2e50656ed47895f00c but I don't get why this cause issues.

cbdt avatar May 11 '25 10:05 cbdt

@Sukairo-02 It must probably be some detail that you missed

yWilliamLima avatar May 12 '25 19:05 yWilliamLima

Was someone able to find a workaround?

kamalbennani avatar May 16 '25 08:05 kamalbennani

I faced the exact same "Property 'config' is protected but type 'Column<...>' is not a class derived from 'Column<...>'" error when using innerJoin (and it seems leftJoin as well, based on this issue). This started happening for me with Drizzle ORM 0.43.x.

What fixed it for me was changing my tsconfig.json:

  • From:
    {
      "compilerOptions": {
        "module": "ESNext",
        "moduleResolution": "ESNext"
      }
    }
    
  • To:
    {
      "compilerOptions": {
        "module": "ES2015", // Also changed this
        "moduleResolution": "bundler"
      }
    }
    

My guess: This issue seems related to how TypeScript resolves Drizzle's type definitions. moduleResolution: "ESNext" (or similar Node strategies) might be causing conflicting or duplicate type inferences for PgTableWithColumns versus the PgTable expected by join methods. This leads to TypeScript seeing them as incompatible.

Switching to moduleResolution: "bundler" likely provides a more consistent type resolution, especially for how Drizzle ORM structures its types.

This tsconfig.json adjustment resolved the type incompatibility for me — though it feels more like a workaround than a real solution, and it’s probably only viable in a constrained setup like turborepo.

HirokiTakekuma avatar May 20 '25 04:05 HirokiTakekuma

The solution @HirokiTakekuma presented sadly did not fix the issue for me

ESchouten avatar May 20 '25 17:05 ESchouten

Any solution or workaround for this issue?

darahask avatar Jun 01 '25 16:06 darahask

@yWilliamLima do you have a monorepo setup?

AndriiSherman avatar Jun 03 '25 13:06 AndriiSherman

reproduced, fixing this issue at least trying

AndriiSherman avatar Jun 03 '25 13:06 AndriiSherman

fixed in [email protected]

AndriiSherman avatar Jun 04 '25 07:06 AndriiSherman

thank you so much

wmodden avatar Jul 01 '25 01:07 wmodden

Still an issue in 0.44.3

ESchouten avatar Jul 18 '25 15:07 ESchouten