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

[BUG]: drizzle-zod schema methods incorrectly set the field types to `Buffer` instead of correct types

Open vlbin opened this issue 5 months ago • 8 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.42.0

What version of drizzle-kit are you using?

0.31.0

Other packages

[email protected]

Describe the Bug

I am trying to use the createSelectSchema and createInsertSchema to produce validation schemas for my sqlite tables.

I have two Hono projects with the same dependencies, in one project it works correctly but in the other one it does not. When I copy the schema file from the non-working project to the working one it works.

Both projects use [email protected] and [email protected] and I have ensured that my { z } import is from zod/v4.

The contents of the tsconfig.json is the same in both projects

{
    "compilerOptions": {
        "target": "ESNext",
        "module": "ESNext",
        "moduleResolution": "Bundler",
        "strict": true,
        "skipLibCheck": true,
        "lib": ["ESNext"],
        "types": ["@cloudflare/workers-types/2023-07-01"],
        "jsx": "react-jsx",
        "jsxImportSource": "hono/jsx",
        "baseUrl": "./",
        "paths": {
            "@/*": ["./src/*"]
        }
    }
}

I have created the following test table in both projects

const exampleTable = sqliteTable('example', {
    id: text('id').primaryKey(),
    name: text('name').notNull(),
    createdAt: integer('created_at', { mode: 'timestamp_ms' }).notNull(),
});

const exampleSchema = createSelectSchema(exampleTable);
type Example = z.infer<typeof exampleSchema>;

In the working project the Example type has the following shape:

type Example = {
    id: string;
    name: string;
    createdAt: Date;
}

and in the non-working it looks like this:

type Example = {
    id: Buffer;
    name: Buffer;
    createdAt: Date;
}

I will also attach my dependencies which are the same across the two projects:

    "dependencies": {
        "@hono/zod-openapi": "^0.19.4",
        "@supabase/supabase-js": "^2.49.4",
        "drizzle-orm": "^0.42.0",
        "drizzle-zod": "^0.8.2",
        "hono": "^4.7.7",
        "hono-openapi": "^0.4.6",
        "nanoid": "^5.1.5",
        "stoker": "^1.4.2",
        "zod": "^3.25.71"
    },
    "devDependencies": {
        "@cloudflare/workers-types": "^4.20250214.0",
        "@eslint/eslintrc": "^3.3.1",
        "@eslint/js": "^9.25.1",
        "@typescript-eslint/eslint-plugin": "^8.31.0",
        "@typescript-eslint/parser": "^8.31.0",
        "drizzle-kit": "^0.31.0",
        "eslint": "^9.25.1",
        "eslint-config-prettier": "^10.1.2",
        "eslint-plugin-prettier": "^5.2.6",
        "prettier": "^3.5.3",
        "wrangler": "^4.4.0"
    }

vlbin avatar Jul 03 '25 07:07 vlbin

Okay I found out that if I import import type { SupabaseClient } from '@supabase/supabase-js'; anywhere in the project, it makes it work. In one project I had not yet set up Supabase, and when I added this import it started working. To confirm this I have tried multiple times to add/remove the import and this is indeed what causes it to work/not work.

vlbin avatar Jul 03 '25 09:07 vlbin

Same issue here. But my project doesn't have supabase and don't know how to make it work

trinvh avatar Jul 17 '25 02:07 trinvh

All types before Buffer when calling .omit() or .extend(). See the screenshot below

Image

tripayfact avatar Jul 18 '25 05:07 tripayfact

Same issue here, if I use .omit() or .extend(), all types are transformed to Buffer, which is very annoying.

dimitarnikolovv avatar Aug 23 '25 00:08 dimitarnikolovv

Hey everyone!

I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.

I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!

We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.


First branch is drizzle-kit rewrite

All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439

As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.

What's left here:

  • We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the srid issue here as well).
  • We are finishing commutative migrations for all dialects.
  • We are finishing up the command, so the migration flow will be as simple as drizzle-kit up for you.

Where it brings us:

  • We are getting drizzle-kit into a new good shape where we can call it [email protected]!

Timeline:

  • We need ~2 weeks to finish all of the above and send this branch to beta for testing.

Second big branch is a complex one with several HUGE updates

  • Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)

TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k

You can read more about it here.

What's left here:

Where it brings us:

  • We are getting drizzle-orm into a new good shape where we can call it [email protected]!

Breaking changes:

  • We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.

Third branch is adding support for CockroachDB and MSSQL dialects

Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.

Summary

All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.

We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).

We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.

Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!

Timelines

We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!

AndriiSherman avatar Aug 30 '25 18:08 AndriiSherman

We run into the same issue with zod 4.1.8, drizzle-orm 0.44.5, and drizzle-zod 0.8.3.

b2m9 avatar Sep 15 '25 12:09 b2m9

same here

Naveenravi07 avatar Sep 21 '25 07:09 Naveenravi07

This is still happening (drizzle-zod 0.8.3, zod 4.0.5, drizzle 0.44.4)...

danieljvdm avatar Nov 10 '25 22:11 danieljvdm

+1

programming-with-ia avatar Nov 15 '25 16:11 programming-with-ia

I also have this problem, so I can't use drizzle-zod to share types with external packages, and .omit and .extend can't be used. Importing from supabase didn't help me.

davidystephenson avatar Nov 18 '25 02:11 davidystephenson

+1

Same here – I am using it in combination with @hono/zod-openapi as well and in one repo it works fine and in the other it doesnt. For example: in my node-environmet it works as it should, infering the correct types, but in my Deno env (Supabase-Edge-Functions), where I import the types via a NPM package, the buffer-bug appears. This is really hard to ignore. It seems only be connected to the type inferences, so the linter yells – the auto-generated types in my api-docs and the validation works fine.

Is there any progress or a hint to solve this issue anytime soon? With our help oc :)

cewald avatar Nov 18 '25 22:11 cewald

I think there's some kind of an issue with declaration merging from other sources. I fixed this issue in my repo by removing cloudflare's worker types from my tsconfig (it was using a generic monorepo tsconfig for server-side code, but the db package didn't need the cloudflare types). The weird thing is that I was trying to reproduce the problem by re-enabling the CF types and I couldn't get them to break again....

So not a super helpful report but I'm sharing in hopes that maybe it can help someone else because my types are now working after not including the cf types in my tsconfig for my db package.

danieljvdm avatar Nov 19 '25 03:11 danieljvdm

I made a little repository where I was able to safely reproduce my version of the problem: https://github.com/cewald/drizzle-orm-issues-4705

As soon as I use createSelectSchema of drizzle-zod to create Zod schemas out of my DB table the proplem arises. It does not matter if NodeJS or Deno in my case. So I guess I will do some investigation on the createSelectSchema method.

cewald avatar Nov 19 '25 15:11 cewald