RobiPoire
RobiPoire
DTDrizzle Team
Created by RobiPoire on 1/27/2025 in #help
CHECK constraints not working with drizzle-orm/pg-core
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`),
})
);
2 replies