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
24.55k stars 645 forks source link

[BUG]:New Drizzle Orm Breaks Typescript Inference In Nest.js Project #3072

Closed kwadwoatta closed 1 month ago

kwadwoatta commented 1 month ago

What version of drizzle-orm are you using?

0.33.0

What version of drizzle-kit are you using?

0.24.2

Describe the Bug

# Type Inference Issues with Drizzle ORM in TypeScript

## Problem Description

TypeScript is having difficulty inferring the correct types from my drizzle-orm schemas, operations, and queries.

## Environment

### tsconfig.json

```json
{
  "compilerOptions": {
    "strict": true,
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": true,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "paths": {
      "@app/common": ["libs/common/src"],
      "@app/common/*": ["libs/common/src/*"]
    }
  }
}

package.json (relevant parts)

{
  "dependencies": {
    "drizzle-orm": "0.34.0",
    // ... other dependencies
  },
  "devDependencies": {
    "drizzle-kit": "0.24.2",
    "typescript": "^5.6.2"
    // ... other dev dependencies
  }
}

Steps Taken

  1. I set "strict": true in tsconfig.json as suggested in another issue for drizzle-orm 0.33.0.
  2. I experimented with other type-related configurations, setting them to true and false, but none resolved the issue.
  3. When noImplicitAny is set to true, I get errors in my schema definition stating that my pgTables are implicitly returning any. This only occurs after the first pgTable I've defined.

Current Issues

  1. No type inference on select operations or querys.
  2. Selects infer type as:
    const existingUser: {
    }[]
  3. Joins infer types as:
    const usersWithFaceEmbeddings: {
    }[]

Additional Context

These issues were not present in an older NestJS project built with drizzle-orm (0.30.8) and drizzle-kit (0.20.17): https://github.com/kwadwoatta/purshew.

Questions

  1. Is this a known issue with the current version of drizzle-orm (0.33.0)?
  2. Are there any specific configuration changes or workarounds to improve type inference?
  3. Could this be related to the upgrade from version 0.30.8 to 0.33.0?

Any assistance or insights would be greatly appreciated.

Expected behavior

Correct type inference

Environment & setup

tsconfig.json

{
  "compilerOptions": {
    "strict": true,
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": true,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "paths": {
      "@app/common": ["libs/common/src"],
      "@app/common/*": ["libs/common/src/*"]
    }
  }
}

package.json (relevant parts)

{
  "dependencies": {
    "drizzle-orm": "0.34.0",
    // ... other dependencies
  },
  "devDependencies": {
    "drizzle-kit": "0.24.2",
    "typescript": "^5.6.2"
    // ... other dev dependencies
  }
}
TheMichio commented 1 month ago

@kwadwoatta most probably you have circular FK references, that's why when you turn on the "noImplicitAny": true and you're able to see the no implicit any errors in your schema file(s). this means, insertion in your table is not possible or not safe, sooner or later you're gonna have issues deleting records I guess, I think you have two options:

  1. make one side of FKs nullable, you get the types back, but then brings us back to the deletion issue, which means you have constraints on both side of the relations, so you can just delete them directly, you have to make one of them null and then remove them.
  2. check your project requirements and update your schemas and database design.

hope it helps ;) cheers

kwadwoatta commented 1 month ago

@TheMichio you were right! Thank you!!