A user visits /posts/tech and expects to see tech posts from 2025. Here’s the code:
/posts/tech
const posts = await prisma.post.findMany({ where: { tags: { some: { name: "tech" } }, date: { gte: new Date("2025-01-01") }, }, include: { tags: true }, });
What changes would make this code also limit results to posts before March 31, 2025?
Add a second findMany call for the date range
findMany
Change gte to lte in the date filter
gte
lte
date
Add tags: { every: { name: "tech" } } to ensure only tech posts
tags: { every: { name: "tech" } }
Add date: { lte: new Date("2025-03-31") } to the where clause
date: { lte: new Date("2025-03-31") }
where
Remove include: { tags: true } to simplify the query
include: { tags: true }
An admin wants to add a post titled "Spring Update" with tags "news" and "updates." The code is:
const post = await prisma.post.create({ data: { title: "Spring Update", content: "New season!", date: new Date(), tags: { connect: [{ name: "news" }, { name: "updates" }] }, }, });
What happens if the "updates" tag doesn’t exist yet in the database?
Prisma creates the "updates" tag automatically
The post is created with no tags
The query fails with an error
The post is created with only the "news" tag
Prisma ignores the tags and logs a warning
You’re debugging the blogging app. A user reports that /posts/news shows no posts, even though news posts exist. Here’s the code:
/posts/news
const posts = await prisma.post.findMany({ where: { tags: { some: { name: "News" } }, date: { gte: new Date("2025-02-01") }, }, });
What could be causing this? (Select all that apply)
The schema forgot to define the tags field in the Post model
tags
Post
The tags relation is missing include: { tags: true }
The tag name "News" is case-sensitive and might be stored as "news"
All news posts are from before February 1, 2025
The database connection URL in .env is wrong
.env
The admin wants to update a post’s title and add a new tag "featured" that doesn’t exist yet. Which code snippet works best?
await prisma.post.update({ where: { id: 1 }, data: { title: "New Title", tags: { create: [{ name: "featured" }] } }, });
await prisma.post.create({ data: { title: "New Title", tags: { connect: { name: "featured" } } }, });
await prisma.post.update({ where: { id: 1 }, data: { title: "New Title", tags: { connectOrCreate: { where: { name: "featured" }, create: { name: "featured" } } } }, });
await prisma.post.update({ where: { id: 1 }, data: { title: "New Title", tags: { connect: { name: "featured" } } }, });
For the client-side route /posts/recent, you want the 5 most recent posts. How would you modify this code?
/posts/recent
const posts = await prisma.post.findMany();
(Select all that apply)
Add orderBy: { date: "desc" }
orderBy: { date: "desc" }
Add take: 5
take: 5
Add sort: { date: "desc" }
sort: { date: "desc" }
Add limit: 5
limit: 5
Add where: { date: { gte: new Date() } }
where: { date: { gte: new Date() } }
This content is only available to registered users.Please register at the the home page.