const userWithPosts = await prisma.user.findUnique({ where: { id: 1 }, include: { posts: true } }); console.log(userWithPosts);
The 'include' option tells Prisma to fetch related records. Here, it fetches all posts related to the user with id 1 and adds them under the 'posts' key.
const post = await prisma.post.findUnique({ where: { id: 5 }, include: { author: true } }); console.log(post.author);
Including 'author: true' fetches the related user who wrote the post. So 'post.author' contains that user's data.
Option B correctly defines the one-to-many relation with the foreign key 'userId' in Post and the relation decorator linking it to User's 'id'.
const users = await prisma.user.findMany({ where: { posts: { id: 1 } } });
When filtering by a relation that is an array, you must use 'some', 'every', or 'none' to specify the condition. Direct nested object causes error.
By default, Prisma does not cascade deletes. It enforces foreign key constraints, so deleting a User with related Posts causes an error unless cascade is explicitly set.
