Define schemas, model relations (1-to-many, many-to-many), run interactive transactions, and use raw SQL safely with Prisma.
0 / 5 completed
1 / 5
What does a Prisma schema file define and how is it used?
Prisma schema: the schema defines the database connection (datasource db { provider = "postgresql" url = env("DATABASE_URL") }), the generator (generator client { provider = "prisma-client-js" }), and models with fields and relations. Running prisma generate creates a fully typed client; prisma migrate dev creates SQL migration files and applies them.
2 / 5
How does Prisma handle one-to-many relations between models?
One-to-many: a Post model has authorId Int and author User @relation(fields: [authorId], references: [id]). The User model has posts Post[]. prisma.user.findUnique({ where: { id }, include: { posts: true } }) returns the user with all their posts in one query, using a SQL JOIN under the hood.
3 / 5
What is an interactive transaction in Prisma and when should it be used?
Interactive transaction:await prisma.$transaction(async (tx) => { const user = await tx.user.create(...); await tx.account.create({ data: { userId: user.id } }); }). If the account creation fails, the user creation is rolled back. Use interactive transactions when operations depend on each other's results — for independent operations, the sequential array form is more efficient.
4 / 5
How does Prisma model many-to-many relations?
Many-to-many: implicit: categories Category[] on Post and posts Post[] on Category — Prisma creates a _CategoryToPost join table. Explicit: define a PostCategory model with postId, categoryId, and extra fields like assignedAt DateTime. Use explicit when the join table needs extra data.
5 / 5
What does prisma.$queryRaw enable and when should it be used over Prisma's generated client?
$queryRaw: complex window functions, CTEs, or database-specific features (PostgreSQL LATERAL JOIN, full-text search with tsvector) may not be expressible through Prisma's fluent API. The tagged template syntax automatically parameterises interpolated values, preventing SQL injection. Use it sparingly — raw queries lose Prisma's type safety at the schema level.