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.44k stars 484 forks source link

[BUG]: query builder does not get any tables when upgrading to anything higher 0.29 #2504

Closed diegoalzate closed 2 weeks ago

diegoalzate commented 2 weeks ago

What version of drizzle-orm are you using?

0.29.5

What version of drizzle-kit are you using?

No response

Describe the Bug

packages:

i have my schema setup in multiple files and they are all exported on schema.ts.

it looks something like this: db/ users.ts likes.ts comments.ts schema.ts <- exports everything

on previous versions (less than v0.28.6) I could pass around the db pool that is created and specify that it will be dbPool: NodePgDatabase<typeof schema> and define it by doing:

import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import * as schema from '../../db/schema';

/**
 * creates a postgres database pool connection
 */
export function createDbPool({
  host,
  port,
  user,
  password,
  database,
}: {
  host: string;
  port?: number;
  user: string;
  password: string;
  database: string;
}) {
  const pool = new Pool({
    host: host,
    port: port,
    user: user,
    password: password,
    database: database,
    ssl: false,
  });

  // the pool will emit an error on behalf of any idle clients
  // it contains if a backend error or network partition happens
  pool.on('error', (err) => {
    console.error('Unexpected error on idle client', err);
    process.exit(-1);
  });

  return {
    pool,
    db: drizzle(pool, { schema }),
  };
}

From version 0.29.5 onwards I can no longer use the query builder it returns the following typescript errors:

No overload matches this call.
  Overload 1 of 3, '(left: Column<ColumnBaseConfig<ColumnDataType, string>, object, object>, right: unknown): SQL<unknown>', gave the following error.
    Argument of type 'PgColumn<{ name: "event_id"; tableName: "cycles"; dataType: "string"; columnType: "PgUUID"; data: string; driverParam: string; notNull: false; hasDefault: false; enumValues: undefined; baseColumn: never; }, {}, {}>' is not assignable to parameter of type 'Column<ColumnBaseConfig<ColumnDataType, string>, object, object>'.
      Types of property 'table' are incompatible.
        Property '[IsDrizzleTable]' is missing in type 'PgTable<TableConfig>' but required in type 'Table<TableConfig<Column<any, object, object>>>'.
  Overload 2 of 3, '(left: Aliased<string>, right: string | SQLWrapper): SQL<unknown>', gave the following error.
    Argument of type 'PgColumn<{ name: "event_id"; tableName: "cycles"; dataType: "string"; columnType: "PgUUID"; data: string; driverParam: string; notNull: false; hasDefault: false; enumValues: undefined; baseColumn: never; }, {}, {}>' is not assignable to parameter of type 'Aliased<string>'.
      Type 'PgColumn<{ name: "event_id"; tableName: "cycles"; dataType: "string"; columnType: "PgUUID"; data: string; driverParam: string; notNull: false; hasDefault: false; enumValues: undefined; baseColumn: never; }, {}, {}>' is missing the following properties from type 'Aliased<string>': sql, fieldAlias
  Overload 3 of 3, '(left: SQLWrapper, right: unknown): SQL<unknown>', gave the following error.
    Argument of type 'PgColumn<{ name: "event_id"; tableName: "cycles"; dataType: "string"; columnType: "PgUUID"; data: string; driverParam: string; notNull: false; hasDefault: false; enumValues: undefined; baseColumn: never; }, {}, {}>' is not assignable to parameter of type 'SQLWrapper'.
      The types returned by 'getSQL()' are incompatible between these types.
        Type 'import("/Users/diegoalzate/Code/work/lexicon/pluraltools-backend/node_modules/.pnpm/drizzle-orm@0.29.5_@types+pg@8.11.6_bun-types@1.1.13_pg@8.12.0_react@18.3.1/node_modules/drizzle-orm/sql/sql").SQL<unknown>' is not assignable to type 'import("/Users/diegoalzate/Code/work/lexicon/pluraltools-backend/node_modules/.pnpm/drizzle-orm@0.28.6_@types+pg@8.11.6_bun-types@1.1.13_pg@8.12.0/node_modules/drizzle-orm/sql/index").SQL<unknown>'.
          Types have separate declarations of a private property 'shouldInlineParams'.ts(2769)

This is fixed by going back down to version 0.28.6. I would like to be up to date with drizzle but I'm not sure why it's broken or how to fix it.

an example of a schema table would be:

import { pgTable, timestamp, uuid, varchar } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: uuid('id').primaryKey().defaultRandom(),
  username: varchar('username', { length: 256 }).unique(),
  firstName: varchar('first_name'),
  lastName: varchar('last_name'),
  email: varchar('email', { length: 256 }).unique(),
  telegram: varchar('telegram', { length: 256 }).unique(),
  createdAt: timestamp('created_at').notNull().defaultNow(),
  updatedAt: timestamp('updated_at').notNull().defaultNow(),
});

export type User = typeof users.$inferSelect;

Expected behavior

I expect that the query builder should not fail when upgrading from 0.28 -> 0.29. I have tried newer versions and it also fails. There should be a guide on how to upgrade this version if it is going to break query lsp

Environment & setup

OS: mac 14.4.1 IDE: vscode NODE: 0.20.14

diegoalzate commented 2 weeks ago

So I came across one of those legendary tsconfig bugs today and it turns out we should just use

https://github.com/tsconfig/bases

my previous config was just using TotalTypescript recommendation when running on node but it actually causes issues when using version 0.29.0 and onwards.