drizzle-orm
drizzle-orm copied to clipboard
[BUG]: drizzle-zod's createInsertSchema() can't handle column of type `vector`
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'
}```
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
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.
Can also confirm that I have the same issue 🤔 is there any quick fix around this except remove the column?
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.
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.
I think this may be happening with the point column type as well in postgres
Facing the same issue with both selectSchema and insertSchema with vector types specifically.
I can confirm the same problem with point type. Is there any workaround? Or maybe a fix, which is better?
Also facing this issue with vector columns when using createSelectSchema and createInsertSchema
I can also confirm that it is still happening with point type from drizzle's pg-core
This is still happening for Point type
"drizzle-orm": "^0.32.0"
"drizzle-zod": "^0.5.1"
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
hi! any updates here? its still happen on "drizzle-kit": "^0.24.2", "drizzle-orm": "^0.33.0", "drizzle-zod": "^0.5.1",
I can confirm it happens for me as well on the latest drizzle zod version
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.
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
Running into this issue using Point
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.
This one was fixed in the latest [email protected]