Query Question on Many to Many :thread:
# orm-help
a
Query Question on Many to Many ๐Ÿงต
โœ… 1
Here is my schema, using Cockroach DB and client 4.0.0
Copy code
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "cockroachdb"
  url      = env("DATABASE_URL")
}

model Player {
  id        String        @id
  username  String?
  createdAt DateTime      @default(now())
  updatedAt DateTime      @default(now())
  losses    Match[]       @relation("Losses")
  resigned  Match[]       @relation("Resigned")
  wins      Match[]       @relation("Wins")
  moves     Move[]
  matches   PlayerMatch[]
  rolls     Roll[]
  lastLogin DateTime?
  access    String?       @default("public")
  email     String?
  password  String?
  updatedBy String?
}

model Match {
  id         String        @id
  status     String        @default("active")
  winnerId   String?
  loserId    String?
  resignedId String?
  createdAt  DateTime      @default(now())
  updatedAt  DateTime      @default(now())
  endedAt    DateTime?
  loser      Player?       @relation("Losses", fields: [loserId], references: [id])
  resigned   Player?       @relation("Resigned", fields: [resignedId], references: [id])
  winner     Player?       @relation("Wins", fields: [winnerId], references: [id])
  moves      Move[]
  rolls      Roll[]
  players    PlayerMatch[]
}

model Move {
  id        BigInt   @id @default(autoincrement())
  matchId   String
  playerId  String
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())
  type      String?
  selected  String   @default("")
  match     Match    @relation(fields: [matchId], references: [id])
  player    Player   @relation(fields: [playerId], references: [id])
}

model Roll {
  id        BigInt   @id @default(autoincrement())
  matchId   String
  playerId  String
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())
  die1      Int
  die2      Int
  match     Match    @relation(fields: [matchId], references: [id])
  player    Player   @relation(fields: [playerId], references: [id])
}

model PlayerMatch {
  playerId  String
  matchId   String
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())
  match     Match    @relation(fields: [matchId], references: [id], onDelete: Cascade)
  player    Player   @relation(fields: [playerId], references: [id], onDelete: Cascade)

  @@id([playerId, matchId])
}
Here's my current query
Copy code
const match = await prisma.match.findUnique({
        where: {
          id: matchId
        },
        include: {
          players: true,
          moves: true,
        }
      });
because the
players
are mapped to
PlayerMatch[]
, i get back
Copy code
"players": [
  {
    "playerId": "bazbar125",
    "matchId": "match-h7rm3yk55rv",
    "createdAt": "2022-06-30T12:57:17.138Z",
    "updatedAt": "2022-06-30T12:57:17.138Z"
  },
  {
    "playerId": "foobar3",
    "matchId": "match-h7rm3yk55rv",
    "createdAt": "2022-06-30T12:57:17.138Z",
    "updatedAt": "2022-06-30T12:57:17.138Z"
  }
],
What I want in that response are the
Players
information, like
username
for example
i want to do something like
Copy code
include: {
          players: {
            select: {
              username: true,
              id: true,
            }
          },
          moves: true,
        }
Got it...
Copy code
include: {
          players: {
           include: {
            player: true,
           }
          },
          moves: true,
        }
๐Ÿš€ 1
๐Ÿ’ฏ 1