Use Prisma Client to Query for Related Data

Ryan Chenkie
InstructorRyan Chenkie
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

When two tables are related to one another, we can use Prisma Client to query for data through those relations. In this lesson, we see how to use Prisma Client to tie together data from two separate tables and pull it out in a nested fashion.

Ryan Chenkie: [0:00] Prisma Studio is aware of the relations between different tables. This makes it easy to manage data. If we add a record for a new review and give it a rating, we can then choose which product we want to assign this review to. Let's say that it should go to the first one, which is shoes.

[0:18] We can save this record, and we now have one review with a relation to a product. If we check out that product, we can see the review that is related. Prisma Client allows us to easily pull out this relation information when we make queries.

[0:33] In the call to the products endpoint to get all the products, add some information to tell Prisma Client that we want to get the review as well. In the findMany call, add an object with the include keyword and say that we want to include reviews true. Save the endpoint and check it out in the browser.

[0:54] Reviews shows up as an array because it is a one-to-many relationship between product and review. For cases where there are no reviews, the array is just empty.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today