P
Prisma2mo ago
MinatoTW

field reference help

hello I have models like this
model Player {
team Int[]
monsters Monster[]
}

model Monster {
id Int
name String
}
model Player {
team Int[]
monsters Monster[]
}

model Monster {
id Int
name String
}
how do I select player.monsters where id is in player.team[] I tried
monsters: { where: { id: { in: player.fields.team } } }
monsters: { where: { id: { in: player.fields.team } } }
but that's throwing type errors
5 Replies
MinatoTW
MinatoTWOP2mo ago
can anyone help?
Nurul
Nurul2mo ago
Hey 👋 Assuming your model looks something like this:
model Player {
id Int @id @default(autoincrement())
team Int[]
monsters Monster[]
}

model Monster {
id Int @id @default(autoincrement())
name String
Player Player? @relation(fields: [playerId], references: [id])
playerId Int?
}
model Player {
id Int @id @default(autoincrement())
team Int[]
monsters Monster[]
}

model Monster {
id Int @id @default(autoincrement())
name String
Player Player? @relation(fields: [playerId], references: [id])
playerId Int?
}
Would a script like this work for your usecase?
const player = await prisma.player.findUnique({
where: { id: playerId },
select: { team: true },
});

const playerWithFilteredMonsters = await prisma.player.findUnique({
where: { id: playerId },
include: {
monsters: {
where: {
id: {
in: player.team,
},
},
},
},
});
const player = await prisma.player.findUnique({
where: { id: playerId },
select: { team: true },
});

const playerWithFilteredMonsters = await prisma.player.findUnique({
where: { id: playerId },
include: {
monsters: {
where: {
id: {
in: player.team,
},
},
},
},
});
MinatoTW
MinatoTWOP2mo ago
hello, thanks for getting back yeah, I was just wondering if it was possible to do this in a single query, but probably not
Nurul
Nurul2mo ago
For this case you would need to use two queries 👍
MinatoTW
MinatoTWOP2mo ago
thanks

Did you find this page helpful?