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.01k stars 606 forks source link

[BUG]: TypeScript error on optional fields during update (and insert) operation #2654

Open DavidHavl opened 3 months ago

DavidHavl commented 3 months ago

What version of drizzle-orm are you using?

0.32.0

What version of drizzle-kit are you using?

0.23.0

Describe the Bug

I've encountered a TypeScript error when trying to update an optional field using the set method. The error occurs only for optional fields, while required fields work as expected.

Steps to reproduce:

Expected behavior: The update operation should work without any TypeScript errors, as all fields being updated are defined in the table schema.

Actual behavior: TypeScript throws an error for the optional 'description' field: "Object literal may only specify known properties, and 'description' does not exist in type" The error does not occur for the required 'title' field.

Additional notes:

Please let me know if you need any additional information or clarification to investigate this issue.

Expected behavior

The update operation should work without any TypeScript errors, as all fields being updated are defined in the table schema.

Environment & setup

Database: SQLite Node: 20 Typescript: 5.3.3

lmcneel commented 3 months ago

We encountered this same issue with:

Database: Postgresql Node: 20 TypeScript: 5.5.3

We attempted to pin to a Drizzle ORM 0.31.0 to wait for a fix and discovered that we can't use Drizzle Kit if we do that. Drizzle Kit prints that it needs to be updated and won't generate migrations. The new version of Drizzle Kit requires Drizzle ORM > 0.32.0.

tirth0 commented 3 months ago

Experiencing the same issue.

Jean-CharlesTerrillon commented 3 months ago

Same here

I suggest to use inferSelect instead of inferInsert, see below

export type PgUpdateSetSource = { [Key in keyof TTable['$inferSelect']]?: GetColumnData<TTable['_']['columns'][Key]> | SQL; } & {};

murilo-kendjy commented 3 months ago

I was facing the same problem when upgrading to version >0.31.4, there's a post in discord where rphlmr said that you need strict mode on your tsconfig.json. That solved for me.

ps: I needed to remove all other strict tags and let only strict tag true.

eliellis commented 3 months ago

I was facing the same problem when upgrading to version >0.31.4, there's a post in discord where rphlmr said that you need strict mode on your tsconfig.json. That solved for me.

ps: I needed to remove all other strict tags and let only strict tag true.

While this does seem to fix the issue, I'd argue that this is an incredibly disruptive change to make to a project when it was not a requirement before now. Hopefully this can be resolved without resorting to flipping strict: true in projects that have been previously successfully using Drizzle without this compiler flag.

mion-kr commented 2 months ago

same issue

khanhquocnguyen commented 2 months ago

I have the same issue.

Environment:

Temporary workaround: Add the following comment line above the insert command. // @ts-ignore: Unreachable code error

Anuolu-2020 commented 1 month ago

Has there being any fix to this bug? I don't want to turn on strict mode in my tsconfig.json

anishkumar127 commented 1 month ago

Same issue please give me solution. any update to fix this issue ?

sameedahri commented 1 month ago

Any solution, I am also facing same issue?

sameedahri commented 1 month ago

I found the solution guys. Just go to the tsconfig.json file, in the compiler options object add below line. "strictNullChecks": true,

anishkumar127 commented 1 month ago

I found the solution guys. Just go to the tsconfig.json file, in the compiler options object add below line. "strictNullChecks": true,

this also not working!

sameedahri commented 1 month ago

Are you guys sure? Because I got stuck for so many days, and this thing solves the issue, I can also insert nullable values and typescript also auto complete table variable name

sameedahri commented 1 month ago

Can you guys share you tsconfig file?

leandromatos commented 1 month ago

Unfortunately, I decided not to upgrade the drizzle-orm and drizzle-kit packages since I don't want to change the typescript configs, which would cause a lot of other troubles in the project.

Here, I'm using drizzle-kit@^0.22.8 and drizzle-orm@^0.31.4.

jabreland commented 4 weeks ago

Changing it to strict worked for me to. (Postrgres and Drizzle-orm 0.33) I was confused because it was working just fine in another project but I had already turned strict on for Acktypes

mogery commented 4 weeks ago

That's not a fix. I'm not going to rewrite a 20k LoC codebase to work with strict null checking just to make this work.

leandromatos commented 4 weeks ago

The problem with using the strict option on TypeScript is that, depending on the size of your code base, you will be forced to update a lot of things. This is really annoying.

naourass commented 3 weeks ago

I was going crazy after updating Drizzle to the latest version. Fortunately I've found that thread, maybe adding a notice in docs beside installation section would help users that were using Drizzle without ts strict mode.

lcptrs commented 2 weeks ago

It seems to have gotten better with the 0.34 release. In our codebase we only have the issue remaining on the set parameter of the onConflictDoUpdate which can be easily worked around with a cast as Partial<typeof table.$inferInsert>🎉

leandromatos commented 2 weeks ago

Unfortunately, @lcptrs, this workaround will add another problem. The result of an insert operation's inference returns the optional and required fields defined in the database. Your suggestion will make all the properties optional.

I believe this problem won't be fixed in the near future. It seems to be a mix of the natural evolution of TypeScript and its config and the evolution of other dependencies. I'm not sure about that, but if the solution is to use strict: true on the tsconfig.json, I think it is because some dependencies use the strict property, and the types are conflicting.

I will probably give up and put strict: true on my project, fix all the TypeScript problems caused by this config, and finally update the new versions of the Drizzle packages.