Hi there, I’m currently working on a small app whe...
# orm-help
p
Hi there, I’m currently working on a small app where the user can create a team that either consist of one player or two players. When I create the team I use
connect
to create a relation to the already existing
player
in the database, which works perfectly. I can’t seem to find how to make this
connect
conditional though. The player 1 will always be there, but player 2 is not always provided (if the team only consists of only one player). Prisma stops record creation that is sees that player 2 is not specified of course. How can I solve this issue? Code below
Copy code
export default async function createTeam(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const teamData = JSON.parse(req.body)
  const team = await prisma.team.create({
    data: {
      player1: {
        connect: {
          id: teamData?.player1?.id,
        },
      },
      player2: {
        connect: {
          id: teamData?.player2?.id,
        },
      },
      groups: {
        connect: {
          id: 'group1-id-test',
        },
      },
    },
  })