intermediate
Schema definitions
Represent tables, columns, constraints, indexes, and relations in TypeScript close to database semantics.
Drizzle schema definitions describe tables, columns, constraints, indexes, and relations in TypeScript. They should mirror database semantics—not become a separate domain model.
export const orders = pgTable('orders', {
id: uuid('id').primaryKey().defaultRandom(),
customerId: uuid('customer_id').notNull().references(() => customers.id),
status: text('status').notNull().default('pending'),
}, (t) => [index('orders_customer_status').on(t.customerId, t.status)]);
On interviews: column types, inferred `$inferSelect` / `$inferInsert`, relations helper, composite indexes, and why constraints still matter.
Common pitfalls: schema code drifting from deployed databases when migrations are skipped or hand-edited without discipline.
The trade-off is TypeScript-native schema versus migration workflow rigor.
Checklist:
- Define constraints in schema.
- Generate useful insert/select types.
- Keep migration state aligned.
- Review provider-specific column types.