CHECK constraints not working with drizzle-orm/pg-core

Hello, I have an issue. I can't add CHECK constraints to my table. I tried with a simple example, but it's not working. Could this be a bug? I followed the code from the documentation, but the CHECK constraints aren't being applied :'( Here is my code: https://drizzle.run/m8kdhex9yw6yqaxbywunfyj4
Code_doc
My_code
1 Reply
RobiPoire
RobiPoireOP4d ago
I found the issue: you should use parentheses instead of square brackets, even though the documentation says to use square brackets.
Here is the correct code:
import { sql } from "drizzle-orm";
import { check, integer, pgTable, text, uuid } from "drizzle-orm/pg-core";

export const users = pgTable(
"users",
{
id: uuid().defaultRandom().primaryKey(),
username: text().notNull(),
age: integer(),
},
(table) => ({
checkConstraint: check("age_check1", sql`${table.age} > 21`),
})
);
import { sql } from "drizzle-orm";
import { check, integer, pgTable, text, uuid } from "drizzle-orm/pg-core";

export const users = pgTable(
"users",
{
id: uuid().defaultRandom().primaryKey(),
username: text().notNull(),
age: integer(),
},
(table) => ({
checkConstraint: check("age_check1", sql`${table.age} > 21`),
})
);

Did you find this page helpful?