Hey there! I’m trying to figure out how to create ...
# orm-help
p
Hey there! I’m trying to figure out how to create many entries without knowing exactly how many beforehand. Example: I’m creating a match which has a number of
sets
. But it can have 2 or 3
sets
depending on how the match went. So how do I create this? If I don’t provide a third set, the query fails. See the
createMany
below:
Copy code
const match = await prisma.match.create({
      data: {
        group: {
          connect: {
            id: groupId,
          },
        },
        team1: {
          connect: {
            id: team1id,
          },
        },
        team2: {
          connect: {
            id: team2id,
          },
        },
        sets: {
          createMany: {
            data: [
              {
                setNumber: 1,
                gamesTeam1: team1_set1,
                gamesTeam2: team2_set1,
                setWinnerId: set1Winner,
              },
              {
                setNumber: 2,
                gamesTeam1: team1_set2,
                gamesTeam2: team2_set2,
                setWinnerId: set2Winner,
              },
              {
                setNumber: 3,
                gamesTeam1: team1_set3,
                gamesTeam2: team2_set3,
                setWinnerId: set3Winner,
              },
            ],
          },
        },
        winner: {
          connect: {
            id: matchWinner,
          },
        },
        date: '2022-01-18T08:56:38.152Z',
      },
    })
e
You could try something like this:
Copy code
sets: {
          createMany: {
            data: [
              {
                setNumber: 1,
                gamesTeam1: team1_set1,
                gamesTeam2: team2_set1,
                setWinnerId: set1Winner,
              },
              {
                setNumber: 2,
                gamesTeam1: team1_set2,
                gamesTeam2: team2_set2,
                setWinnerId: set2Winner,
              },
              ...(team1_set3 && [{
                setNumber: 3,
                gamesTeam1: team1_set3,
                gamesTeam2: team2_set3,
                setWinnerId: set3Winner,
              }]),
            ],
          },
        },
If you have
team1_set3
in your data and it’s not false/0/undefined/null, then the object will be added your sets array. Please note that the object should be in square brackets: https://2ality.com/2017/04/conditional-literal-entries.html
p
Hey Elena! Thank you so much for answering! Unfortunately it still complains - even though
team1_set3
is
null
e
Hmmm… And what is the error? I’m curious why it expects 3 sets 🤔 Is there anything about your schema that demands 3 sets specifically?
p
I don’t really get an error except my api fails - how do I show what prisma complains about?
the thing is a
match
has a relation to a
set
- and it creates a row pr set.
Copy code
model Match {
  id        String   @id @default(cuid())
  groupId   String
  team1Id   String
  team2Id   String
  winnerId  String
  date      DateTime
  createdAt DateTime @default(now()) @map(name: "created_at")
  updatedAt DateTime @updatedAt @map(name: "updated_at")
  group     Group    @relation(references: [id], fields: [groupId])
  team1     Team     @relation("team1", references: [id], fields: [team1Id])
  team2     Team     @relation("team2", references: [id], fields: [team2Id])
  winner    Team     @relation("matchWinner", references: [id], fields: [winnerId])
  sets      Set[]
}
a match can have many sets
but not a defined number of sets
e
Let me PM you
Posting here the correct solution for everyone’s reference:
Copy code
sets: {
  createMany: {
    data: [
      {
        setNumber: 1,
        gamesTeam1: team1_set1,
        gamesTeam2: team2_set1,
        setWinnerId: set1Winner,
      },
      {
        setNumber: 2,
        gamesTeam1: team1_set2,
        gamesTeam2: team2_set2,
        setWinnerId: set2Winner,
      },
        ...team1_set3 ? [{
           setNumber: 3,
           gamesTeam1: team1_set3,
           gamesTeam2: team2_set3,
           setWinnerId: set3Winner,
        }] : [],
      ],
    },
  },
p
Thanks, Elena! 🦜
🙌🏻 1