Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DavidHavl opened this issue Jul 18, 2024 · 34 comments
Closed
Labels
bug Something isn't working priority Will be worked on next

Comments

@DavidHavl
Copy link

DavidHavl commented Jul 18, 2024

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:

  • Define a table with both required and optional fields:
export const TasksTable = sqliteTable(
  'tasks',
  {
    id: text('id')
      .primaryKey()
      .$defaultFn(() => createId()),
    title: text('title').notNull(),
    description: text('description')
  },
);
  • Attempt to update the table with both required and optional fields:
const result = await db
  .update(TasksTable)
  .set({
    title: 'sdfg',
    description: 'asdf'
  });

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:

  • The error only occurs for optional fields (those without .notNull())
  • Using as any type assertion works as a temporary workaround, but it's not a proper solution

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

@DavidHavl DavidHavl added the bug Something isn't working label Jul 18, 2024
@lmcneel
Copy link

lmcneel commented Jul 21, 2024

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
Copy link

tirth0 commented Jul 23, 2024

Experiencing the same issue.

@Jean-CharlesTerrillon
Copy link

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
Copy link

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
Copy link

eliellis commented Jul 24, 2024

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
Copy link

mion-kr commented Aug 1, 2024

same issue

@khanhquocnguyen
Copy link

I have the same issue.

Environment:

  • OS: MacOS
  • Database: PostgreSQL
  • drizzle-orm version: 0.32.2
  • node version: v22.5.1
  • typescript version: 5.4.5

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

@Anuolu-2020
Copy link

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

@anishkumar127
Copy link

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

@sameedahri
Copy link

sameedahri commented Sep 18, 2024

Any solution, I am also facing same issue?

@sameedahri
Copy link

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

@anishkumar127
Copy link

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
Copy link

sameedahri commented Sep 23, 2024

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
Copy link

Can you guys share you tsconfig file?

@leandromatos
Copy link

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
Copy link

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
Copy link

mogery commented Sep 27, 2024

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
Copy link

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
Copy link

naourass commented Oct 3, 2024

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
Copy link

lcptrs commented Oct 8, 2024

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
Copy link

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.

@L-Mario564 L-Mario564 added the priority Will be worked on next label Oct 23, 2024
@0xshc
Copy link

0xshc commented Nov 11, 2024

is there any update on this? still not fixed.

database: postgres
node: 20
typescript: 5.6.3
drizzle-orm: 0.36.1
drizzle-kit: 0.28.0

@NiazMorshed2007
Copy link

same problem :(

drizzle-orm: 0.36.1
drizzle-kit: 0.28.0

@AlexBlokh
Copy link
Contributor

well, that's a very tricky one, we've spent half a day with @Sukairo-02 trying to figure out what type in particular is responsible for that

strict: true indeed fixes it, with strict: false there're 2 more ts flags that fix it, yet we will make sure it works out of the box for you

@AlexBlokh
Copy link
Contributor

alright, landing in beta today - #3542

@ahsankhan201010
Copy link

faced the issue today, love to see that the fix has been merged in beta 🚀

@AndriiSherman
Copy link
Member

fixed in drizzle-orm@0.36.2

@Said-Alisic
Copy link

Still facing the issue and have to use // @ts-ignore: Unreachable code error or something like as Partial<typeof table.$inferUpdate>. Setting "strictNullChecks": true, or removing it raises too many errors across the code that need to be fixed or ignored by the compiler. Really enjoying Drizzle ORM, but have to say it is very wonky at times.

Below is additional information of my file, method and schema structures.

❗ Notice that my field cartItemStatus is not even an optional field as others in this thread mention but it still causes the problem.

package.json

{
  "name": "fitbyprince",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@emotion/cache": "^11.13.1",
    "@emotion/react": "^11.13.0",
    "@emotion/styled": "^11.13.0",
    "@mui/icons-material": "^5.16.7",
    "@mui/lab": "6.0.0-beta.14",
    "@mui/material": "^5.16.7",
    "@mui/material-nextjs": "^5.16.6",
    "@stripe/react-stripe-js": "^2.8.1",
    "@stripe/stripe-js": "^4.9.0",
    "@supabase/ssr": "^0.5.1",
    "@types/luxon": "^3.4.2",
    "@vercel/functions": "^1.4.1",
    "@vidstack/react": "^1.12.11",
    "dotenv": "^16.4.5",
    "drizzle-orm": "^0.36.4",
    "embla-carousel": "^8.1.8",
    "embla-carousel-react": "^8.1.8",
    "embla-carousel-wheel-gestures": "^8.0.1",
    "luxon": "^3.5.0",
    "next": "14.2.5",
    "postgres": "^3.4.4",
    "react": "^18",
    "react-dom": "^18",
    "stripe": "^17.3.1",
    "use-debounce": "^10.0.3"
  },
  "devDependencies": {
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "drizzle-kit": "^0.24.2",
    "eslint": "^8",
    "eslint-config-next": "14.2.5",
    "postcss": "^8",
    "tailwindcss": "^3.4.1",
    "typescript": "^5"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "strictNullChecks": false,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"],
      "@/app/*": ["./src/app/*"],
      "@/enums/*": ["./src/lib/enums/*"],
      "@/interfaces/*": ["./src/libs/interfaces/*"],
      "@/types/*": ["./src/libs/types/*"],
      "@/utils/*": ["./src/libs/utils/*"],
      "@/ui/*": ["./src/libs/ui/*"],
      "@/styles/*": ["./src/libs/styles/*"],
      "@/server/*": ["./src/libs/server/*"],
      "@/client/*": ["./src/libs/client/*"],
      "@/drizzle/*": ["./src/libs/server/drizzle/*"],
      "@/supabase/*": ["./src/libs/server/supabase/index.ts"],
      "@/actions/*": ["./src/libs/server/actions/*"],
      "@/libs/*": ["./src/libs/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

Drizzle Schema

// CartItem Table
export const cartItems = pgTable("cartItems", {
  ...baseSchema,
  ...timestampedSchema,
  itemId: uuid("itemId").notNull(),
  quantity: integer("quantity").notNull().default(1),
  productType: productTypesEnum("productType").notNull(),
  profileId: uuid("profileId")
    .notNull()
    .references(() => profiles.id, { onDelete: "cascade" }),
  cartItemStatus: cartItemStatusesEnum("cartItemStatus")
    .notNull()
    .default("PENDING"),
});

My method

  async updateCartItemsStatusByProfileId(params: { profileId: string, newCartItemStatus: CartItemStatus, options?: { cartItemStatus?: CartItemStatus } }): Promise<CartItemsUpdate> {
    const { profileId, newCartItemStatus, options } = params;

    // NOTE: Temporary fix for false fix at -> https://github.com/drizzle-team/drizzle-orm/issues/2654#issuecomment-2475787921
    // @ts-ignore: Unreachable code error
    const query = db.update(cartItems).set({ cartItemStatus: newCartItemStatus });

    if (options?.cartItemStatus) {
      query.where(eq(cartItems.cartItemStatus, options?.cartItemStatus));
    }

    query.where(eq(cartItems.profileId, profileId));

    return await query;

  }

@leandromatos
Copy link

I'm experiencing the same here, @Said-Alisic 😢

@andyngdz
Copy link

I'm also facing the same issue: when using insert, it doesn't recognize optional fields.

 "drizzle-orm": "^0.36.4"

@kylesloper
Copy link

This is still an issue at 0.36.4

@dancalderon
Copy link

This is still an issue at 0.36.4we should not close this issue yet.

@dexxiez
Copy link

dexxiez commented Dec 15, 2024

still got it in 0.38.2. if anyone here just wants the issue to go away at some expense of elegency and probs some type safety of sorts and just move on with your life, I made this and it's getting me by so far. Probs can make it better but wasted enough time on the issue tbh.

// yeah
export const df = <T>(value: Partial<T>): T => value as T;


// Usage
export type User = InferSelectModel<typeof users>; // Just put these in your schema anyway if you don't have it.

await this.drizzle
  .update(users)
  .set(
    df<User>({
      verification_code: verificationCode,
      verification_code_expires: expiryDate,
    })
  )
  .where(eq(users.email, email));

@ivawzh
Copy link

ivawzh commented Dec 16, 2024

Can we reopen this issue? It is still happening with drizzle-orm 0.38.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority Will be worked on next
Projects
None yet
Development

No branches or pull requests