Prisma Client通过Schema查询连接表中是否存在值



在我的例子中,我有一个模式加入奖金到赌场。查询对数据非常有效,但我无法通过查询本身进行过滤。我使用的where子句似乎是正确的,但我得到了一个错误,陈述对象文字可能只指定已知的属性,并且'nodeposit'在类型中不存在。但是我可以查询这些数据。

const data = await prisma.casino_p_casinos.findMany({
where: { 
approved: 1, 
rogue: 0,
bonuses: {
nodeposit: { gt : 0 },
}
},
select: {
id: true,
clean_name: true,
casino: true,
button: true,
bonuses: {
where: {
nodeposit: { gt: 0 },
},
},
},
take: 14,

});

如果我在WHERE子句中删除奖金项,查询将按预期工作,但我想要获取每个赌场的所有奖金,但仅当奖金包含nodeposit值时。

这就是我想用的。

const data = await prisma.casino_p_casinos.findMany({
where: { 
approved: 1, 
rogue: 0,
bonuses: {
nodeposit: { gt : 0 },
},
},
select: {
id: true,
clean_name: true,
casino: true,
button: true,
bonuses: true,
},
take: 14,

});

模式:

model casino_p_casinos {
id            Int                             @id @default(autoincrement())
casino        String?
type          String?
url           String?
bonuses       casino_p_bonus[]
model casino_p_bonus {
id               Int              @id @default(autoincrement())
parent           Int
game             String?
freespins        Int?
freeplay         String?
nodeposit        Int?
deposit          Int?

casino_p_casinos casino_p_casinos @relation(fields: [parent], references: [id])
}

你有一个一对多的关系,所以当你添加一个where子句时,你有一个与some,everynone的多层,如

const data = await prisma.casino_p_casinos.findMany({
where: {
approved: 1,
rogue: 0,
bonuses: {
// 'some' can be replaced by 'every' or 'none' here
some: {
nodeposit: { gt: 0 }
}
}
},
select: {
id: true,
clean_name: true,
casino: true,
button: true,
bonuses: true
},
take: 14
})

此查询将过滤casinos,其中一些nodeposit大于0,并返回所有奖金,甚至等于0的奖金。

然后,如果你只想要bonusesnodeposit大于0的赌场,你应该这样做:

const data = await prisma.casino_p_casinos.findMany({
where: {
approved: 1,
rogue: 0,
bonuses: {
// 'some' can be replaced by 'every' or 'none' here
some: {
nodeposit: { gt: 0 }
}
}
},
select: {
id: true,
clean_name: true,
casino: true,
button: true,
bonuses: {
where: {
nodeposit: { gt: 0 }
}
}
},
take: 14
})