drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: Make drizzle-zod respect CHECK constraints
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### 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.36.3
### What version of `drizzle-kit` are you using?
0.28.1
### Other packages
drizzle-zod@0.5.1
### Describe the Bug
I used the following SQL to create a table in postgres:
```
CREATE TABLE IF NOT EXISTS test_users
(
id SERIAL PRIMARY KEY,
first_name TEXT NOT NULL CHECK (LENGTH(TRIM(first_name)) BETWEEN 2 and 100),
last_name TEXT NOT NULL CHECK (LENGTH(TRIM(last_name)) BETWEEN 2 and 100)
);
```
This resulted in the following generated code when running `drizzle-kit pull`:
```
export const testUsers = pgTable("test_users", {
id: serial().primaryKey().notNull(),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
}, (table) => {
return {
testUsersFirstNameCheck: check("test_users_first_name_check", sql`(length(TRIM(BOTH FROM first_name)) >= 2) AND (length(TRIM(BOTH FROM first_name)) <= 100)`),
testUsersLastNameCheck: check("test_users_last_name_check", sql`(length(TRIM(BOTH FROM last_name)) >= 2) AND (length(TRIM(BOTH FROM last_name)) <= 100)`),
}
});
```
When I use `drizzle-zod` for type safety before doing inserts:
```
export const testUsersInsertSchema = createInsertSchema(testUsers);
```
I can see that the types are not taking into consideration the check constraints:
```
export const testUsersInsertSchema: ZodObject<{ id: ZodOptional firstName: ZodString lastName: ZodString }, UnknownKeysParam, ZodTypeAny, {}, {}>
````
When I first found `drizzle-zod`, I was super excited, as it would cut out the `zod` boilerplate schemas on my client/server to match my database's generated drizzle schema. However to my surprise, check constraints aren't honored, and that really diminishes the value of the library. Forms that have all empty strings pass all checks, and so you are forced to "refine" your entire schema anyway. A fix for this would be fantastic.
Contributor guide
Assessment
This issue has not been assessed yet.