Search code examples
postgresqlprismatrpc.io

How to get the name of the Tool's ToolCategory?


So I have a PostgreSQL schema written in Prisma, which looks like this:

model Tool {
  id               String       @id @default(cuid())
  name             String
  description      String
  url              String
  documentationUrl String?
  user             User         @relation(fields: [userId], references: [id])
  userId           String
  toolCategory     ToolCategory @relation(fields: [toolCategoryId], references: [id])
  toolCategoryId   String
}

model ToolCategory {
  id          String @id @default(cuid())
  name        String
  description String
  tools       Tool[]
}

I can query all the properties of Tool, like description, toolCategoryId, etc. Still, I cannot figure out how I could access the name of its category (ToolCategory.name) by only querying Tool.


Solution

  • You should be able to do that with either select:

    prisma.tool.findMany({
      select: { id: true, name: true, toolCategory: { select: { name: true } } },
    });
    

    or include:

    prisma.tool.findMany({
      include: { toolCategory: { select: { name: true } } },
    });