drizzle-team / drizzle-team/drizzle-orm

[BUG]: Cannot use filters in `check` constraints

Open
#3,144 2 comments 1 reaction 0 assignees View on GitHub
drizzle/kit improvement priority
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

0.35.1

### What version of `drizzle-kit` are you using?

0.26.2

### Describe the Bug

```ts
import { gte } from "drizzle-orm";
import {
check,
integer,
pgTable
} from "drizzle-orm/pg-core";

export const test = pgTable(
"users",
{
id: integer("id").primaryKey(),
num: integer("num").notNull().default(0),
},
(table) => ({
noNegativeNum: check("noNegativeNum", gte(table.num, 0)),
})
);

```

this schema generates this invalid migration:

```sql
CREATE TABLE IF NOT EXISTS "users" (
"id" integer PRIMARY KEY NOT NULL,
"num" integer DEFAULT 0 NOT NULL,
CONSTRAINT "noNegativeNum" CHECK ("users"."num" >= $1)
);
```

Which fails to execute because of the `$1` param included in the place of the actual value `0`

## Workaround

if declared with the `sql` operator -- [as documented](https://orm.drizzle.team/docs/indexes-constraints#check), to be fair -- it works as intended

```ts
(table) => ({
noNegativeNum: check("noNegativeNum", sql`${table.num} >= 0`),
})
```

I'm not entirely sure if filters were supposed to be supported like that, but when learning the drizzle API, my first intuition was to use them when declaring constraints too. So it would probably be best for filters to be supported in there too for consistency.

### Expected behavior

It should generate a valid migration without the placeholder

### Environment & setup

Not relevant

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.