I have table User , which: - has relationship 1:m...
# orm-help
b
I have table User , which: • has relationship 1:m with posts (defined through @relation) • has relationship m:1 with UserType (defined through @relation) and I would like to write a query which would return: • name of User • count of total number of related posts • UserType.label I was able to write a query to fet name and count but not include UserType properly. I could include type_id but thats identifier and I would have to label:
Copy code
return this.prisma.users.findMany({
      select: {
        name: true,
        type_id: true,
        _count: {
          select: {
            posts: true
        }
      }
      }
    });
  }
}
n
Hey Brothak 👋 thanks a lot for your message! Could you maybe share the relevant parts of your Prisma schema so that we can help you figure out the right query for this? 🙂
b
@nikolasburk sure and thanks! Any recommended tool for sharing the schema?
I posted it to pastie for now https://www.pastiebin.com/6203c8ea72b81
n
You can also just drop it here on Slack enclosing it with three backticks 🙂
Copy code
model Post {
  id              Int             @id @default(autoincrement())
  user_id         Int
  created_at      DateTime        @default(now()) @db.Timestamptz(6)
  updated_at      DateTime        @default(now()) @db.Timestamptz(6)
  user            User @relation(fields: [user_id], references: [id], onDelete: NoAction, map: "fk_users")
}

model User {
  id          Int         @id @default(autoincrement())
  name        String      @db.VarChar(255)
  type_id      String      @db.VarChar(255)

  created_at  DateTime    @default(now()) @db.Timestamptz(6)
  updated_at  DateTime    @default(now()) @db.Timestamptz(6)
  posts        Post[]
  type         UserType   @relation(fields: [type_id], references: [id], onDelete: NoAction, map: "fk_user_type")

  @@unique([name, type_id])
}

model UserType {
  id         String   @unique @db.VarChar(255)
  label      String   @unique @db.VarChar(255)
  created_at DateTime @default(now()) @db.Timestamptz(6)
  updated_at DateTime @default(now()) @db.Timestamptz(6)
  Users User[]
}
b
hh true, bit used to IRC 🙂
was hoping there is something syntax highlighting
n
Haha yes syntax highlighting would def be nice here on Slack (it does exist on GitHub for Prisma schemas though )!
Based on your info, I think this query should work for you now:
Copy code
const users = await prisma.user.findMany({
  select: {
    name: true,
    type_id: true,
    type: {
      select: {
        label: true,
      },
    },
    _count: {
      select: {
        posts: true,
      },
    },
  },
});
You can do nested selects, so that’s how you can also get the
label
of the related
UserType
.
b
Cheers thanks, just fyi been following prisma here and there and was hesitant to use in production and just sheer amount of free support Prisma team provides is amazing and one of main reasons to convince me to give any ORM try after years
prisma rainbow 1
💚 1
n
Ha, that’s really nice to hear! Thanks so much for the feedback 🙌