I tried having a `friendship` model that had an ar...
# orm-help
t
I tried having a
friendship
model that had an array of user IDs, or an array of users, but the problem was to fetch all of user A's friends, I could easily get all the IDs, but then I had to perform 1 query for each friend to fetch the whole user model. It worked but it seemed very inefficient - is there a better way or is this the only way it can work?
👀 1
l
with prisma you can pull the related model at the same time with the
include
field in the options. basically a JOIN in the query. These can be nested to retrieve most data in one operation:
Copy code
const result = prisma.user.findFirst({
  where: {
    id: "1234",
  },
  include: {
    friendships: {
      include: {
        user: true,
      },
    },
  },
});
I think there is a section on this in the docs: https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#include-deeply-nested-relations