Define type-safe schemas, write compile-time-verified queries, manage migrations with Drizzle Kit, declare relations, and use raw SQL safely.
0 / 5 completed
1 / 5
How does Drizzle ORM define a database schema and what does it generate?
Drizzle schema: the schema file is the single source of truth for both types and SQL structure. typeof users.$inferSelect gives the TypeScript type for a selected row; typeof users.$inferInsert gives the insert type. No code generation step is needed — the types are computed at TypeScript compilation time from the schema definitions.
2 / 5
How does Drizzle's query builder achieve type safety?
Drizzle type safety:eq(users.age, "not a number") is a TypeScript compile error if age is an integer column. Selecting specific columns — db.select({ name: users.name }).from(users) — returns { name: string }[], not the full user type. Join results merge the selected column types automatically, with no runtime overhead.
3 / 5
How does Drizzle Kit handle database migrations?
Drizzle Kit:drizzle-kit generate diffs the TypeScript schema against the last migration snapshot and produces a SQL migration file: ALTER TABLE users ADD COLUMN email text NOT NULL. These files are committed to version control. drizzle-kit push (for development) applies schema changes directly without generating migration files — useful for rapid prototyping.
4 / 5
How does Drizzle ORM handle relations between tables?
Drizzle relations: the relations definition is not reflected in SQL — it's metadata for the Drizzle query API. db.query.users.findMany({ with: { posts: true } }) automatically joins and returns users with their posts typed correctly. The underlying SQL schema still uses foreign key columns; the relations() declaration just teaches Drizzle how to navigate them.
5 / 5
How does Drizzle ORM support raw SQL when the query builder is insufficient?
Drizzle raw SQL: the sql template tag is also usable inline: db.select({ rank: sql<number>`rank() OVER (ORDER BY score DESC)` }).from(scores) embeds a raw SQL window function into a typed query. Interpolated values become prepared statement parameters — never string-concatenated — keeping the type safety guarantees even in raw SQL usage.