drizzle-team / drizzle-team/drizzle-orm
[BUG]: Cannot use filters in `check` constraints
- 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
Assessment
This issue has not been assessed yet.