Reputation: 61
I have created a phone number field within a table using Drizzle. I want to ensure that the table only accepts phone numbers that contain numbers and have a length of 10. I made a constraint based on responses to similar questions, but once I push the change and re-seed the table, it is clear the constraints are not being considered. While taking a break from this problem, I also determined I am struggling to set a minValue for the id field as well. My code is below as well as a picture of the data that gets seeded into the table. I made a constraint for the email as well, and that seemed to work.
import { serial, integer, text, boolean, pgTable, timestamp, check } from "drizzle-orm/pg-core";
import { sql } from 'drizzle-orm'
export const userTable = pgTable("user", {
id: serial('id').primaryKey(),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
email: text("email").notNull().unique(),
phoneNumber: text("phone_number").notNull().unique(),
password: text("password").notNull(),
isAdmin: boolean("is_admin").notNull().default(false),
createdAt: timestamp("created_at").notNull().default(new Date())
},
(table) => [{
numberLengthConstraint: check('phone_number', sql`${table.phoneNumber}.length === 10`),
numberContentConstraint: check('phone_number', sql`${table.phoneNumber}~'^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$`),
emailConstraint: check('email', sql`${table.email}~'^[/^\S+@\S+\.\S+$/]`)
}]
);
Any assistance is appreciated.
Upvotes: 1
Views: 110