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

[BUG]: drizzle-zod's createInsertSchema() can't handle column of type `vector`

Open sp88011 opened this issue 1 year ago • 14 comments

What version of drizzle-orm are you using?

0.31.0

What version of drizzle-kit are you using?

0.22.1

Describe the Bug

One of my columns is of type vector (from the pgvector extension). I use createInsertSchema() from the drizzle-zod package to create a Zod schema for that table. When navigating to the page where that Zod schema is used, I'm getting the following error:

Error Cannot use 'in' operator to search for 'enumValues' in undefined

When I remove the vector column, everything works fine. I'm not really sure why the error talks about enumValues at all..but I figured it must have something to do with the vector column.

export const productSchema = createInsertSchema(T_product);
...

schema:

export const T_product = pgTable(
  "product",
  {
    id: varchar("id", { length: 36 })
      .$defaultFn(() => uuidv7())
      .primaryKey(),
    createdAt: timestamp("createdAt").defaultNow(),
    updatedAt: timestamp("updatedAt"),
    name: text("name").notNull(),
    embedding: vector("embedding", { dimensions: 384 }),
//...
  },
  (t) => ({
    embeddingIdx: index("embedding_idx").using(
      "hnsw",
      t.embedding.op("vector_cosine_ops"),
    ),
  }),
);

Expected behavior

Error should not happen.

Environment & setup

I'm using "drizzle-zod": "^0.5.1"

Full Error stack:

⨯ TypeError: Cannot use 'in' operator to search for 'enumValues' in undefined
    at eval (webpack-internal:///(rsc)/./node_modules/drizzle-zod/index.mjs:17:1262)
    at p (webpack-internal:///(rsc)/./node_modules/drizzle-zod/index.mjs:17:1319)
    at p (webpack-internal:///(rsc)/./node_modules/drizzle-zod/index.mjs:17:1789)
    at eval (webpack-internal:///(rsc)/./node_modules/drizzle-zod/index.mjs:17:564)
    at Array.map (<anonymous>)
    at c (webpack-internal:///(rsc)/./node_modules/drizzle-zod/index.mjs:17:547)
    at eval (webpack-internal:///(rsc)/./utils/zodSchemas.ts:65:88)
    at (rsc)/./utils/zodSchemas.ts (/Users/j/Documents/webdev/market-analytics/.next/server/app/admin/entities/page.js:1944:1)
    at __webpack_require__ (/Users/j/Documents/webdev/market-analytics/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./app/admin/entities/validations.ts:15:75)
    at (rsc)/./app/admin/entities/validations.ts (/Users/j/Documents/webdev/market-analytics/.next/server/app/admin/entities/page.js:1370:1)
    at __webpack_require__ (/Users/j/Documents/webdev/market-analytics/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./app/admin/entities/page.tsx:7:70)
    at (rsc)/./app/admin/entities/page.tsx (/Users/j/Documents/webdev/market-analytics/.next/server/app/admin/entities/page.js:1359:1)
    at Function.__webpack_require__ (/Users/j/Documents/webdev/market-analytics/.next/server/webpack-runtime.js:33:43)
    at async e9 (/Users/j/Documents/webdev/market-analytics/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:396554)
    at async tb (/Users/j/Documents/webdev/market-analytics/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:400212)
    at async tS (/Users/j/Documents/webdev/market-analytics/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:400773)
    at async tS (/Users/j/Documents/webdev/market-analytics/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:400904)
    at async tS (/Users/j/Documents/webdev/market-analytics/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:400904)
    at async tS (/Users/j/Documents/webdev/market-analytics/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:400904)
    at async tR (/Users/j/Documents/webdev/market-analytics/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:36:2130)
    at async /Users/j/Documents/webdev/market-analytics/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:36:2722 {
  digest: '3542844501',
  page: '/admin/products'
}```

sp88011 avatar Jun 02 '24 23:06 sp88011

Experiencing the same error after updating to latest drizzle-orm 0.31.0.

The problems seems to be that the vector column is defined as type: array but doesn't expose a baseColumn value that is used by drizzle-zod here

matthiasbayer avatar Jun 04 '24 14:06 matthiasbayer

Same here. Followed this official guide (https://orm.drizzle.team/learn/guides/vector-similarity-search) and using vector breaks my code when I'm using createInsertSchema on that table.

jgontrum avatar Jun 06 '24 14:06 jgontrum

Can also confirm that I have the same issue 🤔 is there any quick fix around this except remove the column?

feliche93 avatar Jun 11 '24 13:06 feliche93

I experienced as well. As a temporary solution, I defined the zod schema manually for just my embedding table.

I have the content in one table, then when a new row gets added, it is broken into chunks and each chunk is saved in another table (embeddings) that has a FK to the id of the content.

nicoalbanese avatar Jun 11 '24 13:06 nicoalbanese

FYI for those who are blocked because of this, I was able to avoid this issue by creating a new embeddings table and joining them onto my profiles. Which is probably a better way for our project in the long run.

mattiasbonte avatar Jun 13 '24 17:06 mattiasbonte

I think this may be happening with the point column type as well in postgres

waltershewmake avatar Jun 22 '24 18:06 waltershewmake

Facing the same issue with both selectSchema and insertSchema with vector types specifically.

diego-escobedo avatar Jun 26 '24 19:06 diego-escobedo

I can confirm the same problem with point type. Is there any workaround? Or maybe a fix, which is better?

yantakus avatar Jun 28 '24 05:06 yantakus

Also facing this issue with vector columns when using createSelectSchema and createInsertSchema

dpromisel avatar Jul 08 '24 15:07 dpromisel

I can also confirm that it is still happening with point type from drizzle's pg-core

ludersGabriel avatar Jul 09 '24 14:07 ludersGabriel

This is still happening for Point type

"drizzle-orm": "^0.32.0"
"drizzle-zod": "^0.5.1"

danielivanovz avatar Jul 12 '24 17:07 danielivanovz

for now you can't co-locate the zod schemas in any files that export schemas. you will have to have the schemas in a separate file for now

austinm911 avatar Sep 01 '24 20:09 austinm911

hi! any updates here? its still happen on "drizzle-kit": "^0.24.2", "drizzle-orm": "^0.33.0", "drizzle-zod": "^0.5.1",

timurkhakhalev avatar Sep 26 '24 20:09 timurkhakhalev

I can confirm it happens for me as well on the latest drizzle zod version

Matangub avatar Oct 03 '24 15:10 Matangub

Downgrading to [email protected] resolved it for me in the interim

npm i [email protected]

Brenndoerfer avatar Oct 08 '24 16:10 Brenndoerfer

Downgrading to [email protected] resolved it for me in the interim

npm i [email protected]

Unfortunately this causes a regression in other bugs for example postgres integers being types as unknown.

CalNicklin avatar Nov 04 '24 10:11 CalNicklin

update to latest below and once i try to add embedding vector field i get the Cannot use 'in' operator to search for 'enumValues' in undefined. other wise it generates the schema :/ any ideas ? "drizzle-kit": "^0.28.1", "drizzle-orm": "^0.36.3", "drizzle-zod": "^0.5.1",

update: moved the drizzle-zod to another files as someone suggested and orm stopped crashing on migration generation. thanks for the tip

orenmizr avatar Nov 18 '24 17:11 orenmizr

Running into this issue using Point

kedom1337 avatar Dec 05 '24 10:12 kedom1337

I got the same error:

TypeError: Cannot use 'in' operator to search for 'enumValues' in undefined
    at isWithEnum (/Users/kiki/workspaces/pos-service/node_modules/src/index.ts:192:9)
    at mapColumnToSchema (/Users/kiki/workspaces/pos-service/node_modules/src/index.ts:198:6)
    at mapColumnToSchema (/Users/kiki/workspaces/pos-service/node_modules/src/index.ts:210:19)
    at map (/Users/kiki/workspaces/pos-service/node_modules/src/index.ts:116:17)
    at Array.map (<anonymous>)
    at exports.createInsertSchema (/Users/kiki/workspaces/pos-service/node_modules/src/index.ts:115:55)
    at Object.<anonymous> (/Users/kiki/workspaces/pos-service/src/services/AddressService.ts:8:48)
    at Module._compile (node:internal/modules/cjs/loader:1358:14)
    at Module.m._compile (/Users/kiki/workspaces/pos-service/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
Waiting for the debugger to disconnect...
Waiting for the debugger to disconnect...

My schema was generated by the drizzle-kit pull command:

export const addresses = pgTable("addresses", {
  id: varchar()
    .default(sql`custom_uuid()`)
    .primaryKey()
    .notNull(),
  address1: varchar({ length: 100 }),
  address2: varchar({ length: 100 }),
  address3: varchar({ length: 100 }),
  city: varchar({ length: 50 }),
  state: varchar({ length: 50 }),
  country: varchar({ length: 50 }),
  post_code: varchar({ length: 15 }),
  geo_location: point(),
  address_type: varchar({ length: 50 }),
  created_at: timestamp({ mode: "string" }).default(sql`CURRENT_TIMESTAMP`),
  updated_at: timestamp({ mode: "string" }).default(sql`CURRENT_TIMESTAMP`),
  is_deleted: boolean().default(false),
});

The zod definition code is:

import { createInsertSchema } from "drizzle-zod";
export const AddressSchema = createInsertSchema(addresses);

I think it is caused by the point type of PG. My temporary solution is to use the original Zod definition:

export const AddressSchema = z.object({
  id: z.string().optional(),
  address: z.string().optional(),
  address2: z.string().optional(),
  address3: z.string().optional().nullable(),
  city: z.string().optional(),
  state: z.string().optional(),
  country: z.string().optional(),
  post_code: z.string().optional(),
  geo_location: z.object({}).optional().nullable(),
  address_type: z.string().optional().nullable(),
	is_deleted: z.boolean().default(false).optional().nullable(),
});

Hoping this issue can be addressed soon.

derrick-null avatar Dec 05 '24 19:12 derrick-null

This one was fixed in the latest [email protected]

AndriiSherman avatar Dec 09 '24 15:12 AndriiSherman