Prisma ORM helps you easily work with databases in your Next.js app. It turns database data into simple JavaScript objects you can use.
You want to save user data like names and emails in a database.
You need to read and show data from a database on your website.
You want to update or delete data safely without writing complex SQL.
You want to keep your database code clean and easy to understand.
Syntax
NextJS
npx prisma init
// Then edit prisma/schema.prisma to define your data models// Generate Prisma client
npx prisma generate
// Use Prisma client in your Next.js codeimport { PrismaClient } from'@prisma/client'const prisma = new PrismaClient()
npx prisma init creates the basic setup files.
You define your database tables as models in schema.prisma.
Examples
This defines a User table with id, name, and unique email.
NextJS
model User {
id Int @id @default(autoincrement())
name String
email String @unique
}
B. An array of all posts regardless of published status
C. An error because 'published' is not a valid field
D. An array of posts where published is true
Solution
Step 1: Understand the Prisma model and query
The model has a Boolean field published. The query filters posts where published is true.
Step 2: Analyze the query result
findMany returns an array of all matching posts, so it returns all posts with published: true.
Final Answer:
An array of posts where published is true -> Option D
Quick Check:
findMany with where filter = array filtered by condition [OK]
Hint: findMany returns arrays filtered by where conditions [OK]
Common Mistakes:
Expecting a single object instead of an array
Ignoring the where filter and expecting all posts
Thinking 'published' field does not exist
4. You wrote this code in a Next.js API route:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export default async function handler(req, res) {
const user = await prisma.user.findUnique({ where: { id: req.query.id } });
res.json(user);
}
But you get an error: TypeError: Cannot read property 'id' of undefined. What is the likely cause?
medium
A. PrismaClient is not instantiated correctly
B. The findUnique method does not exist
C. The req.query object is undefined or missing id
D. You forgot to import res from Next.js
Solution
Step 1: Identify the error cause
The error says Cannot read property 'id' of undefined, meaning req.query is undefined or id is missing.
Step 2: Check the API route usage
Ensure the request URL includes a query parameter ?id=someValue so req.query.id exists.
Final Answer:
The req.query object is undefined or missing id -> Option C
Quick Check:
Missing query param causes undefined error [OK]
Hint: Check if query parameters exist before accessing them [OK]
Common Mistakes:
Assuming PrismaClient instantiation causes this error
Thinking findUnique method is invalid
Trying to import res object explicitly
5. You want to add a new model Comment related to Post in your Prisma schema, where each comment belongs to one post. Which of the following schema additions correctly sets up this relationship?
hard
A. model Comment {
id Int @id @default(autoincrement())
content String
postId Int
post Post @relation(fields: [postId], references: [id])
}
model Post {
id Int @id @default(autoincrement())
title String
comments Comment[]
}
B. model Comment {
id Int @id @default(autoincrement())
content String
post Post
}
model Post {
id Int @id @default(autoincrement())
title String
}
C. model Comment {
id Int @id @default(autoincrement())
content String
postId Int
}
model Post {
id Int @id @default(autoincrement())
title String
comments Comment[]
}
D. model Comment {
id Int @id @default(autoincrement())
content String
postId Int
post Post
}
model Post {
id Int @id @default(autoincrement())
title String
comments Comment[]
}
Solution
Step 1: Understand Prisma relations syntax
To relate Comment to Post, Comment needs a foreign key postId and a relation field post with @relation specifying fields and references.
Step 2: Check the Post model for back relation
Post should have a comments field as an array of Comment to represent one-to-many relation.
Final Answer:
Option A correctly defines the foreign key, relation, and back relation -> Option A
Quick Check:
Relation fields with @relation and back array = model Comment {
id Int @id @default(autoincrement())
content String
postId Int
post Post @relation(fields: [postId], references: [id])
}
model Post {
id Int @id @default(autoincrement())
title String
comments Comment[]
} [OK]
Hint: Use @relation with fields and references for foreign keys [OK]
Common Mistakes:
Omitting @relation attribute causing errors
Not defining back relation array in Post
Missing foreign key field postId in Comment
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.