drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Make drizzle-zod respect CHECK constraints

Open
#3,600 4 comments 4 reactions 0 assignees View on GitHub
drizzle/typebox drizzle/valibot drizzle/zod enhancement
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.