Prisma js ORM - 如何过滤在相关 table(有效 JOIN)中有条目的结果?
Prisma js ORM - how to filter for results that have entry in a related table (effectively JOIN)?
我有 2 个表:
model Collection {
id String @id @default(uuid()) @db.Uuid/
floorPrices CollectionFloorPrice[]
}
model CollectionFloorPrice {
id String @id @default(uuid()) @db.Uuid
collection Collection @relation(fields: [collectionId], references: [id])
collectionId String @db.Uuid
}
如何查询只有 CollectionFloorPrice
中存在行的集合?在 SQL 中,这将是一个简单的 JOIN。
这不起作用:
return await this.prisma.collection.findMany({
where: {
floorPrices: {
exists: true,
},
},
});
名为 CollectionFloorPrice
的模型的 Prisma 关系过滤器是:
export type CollectionFloorPriceFilter = {
every?: CollectionFloorPriceWhereInput | null
some?: CollectionFloorPriceWhereInput | null
none?: CollectionFloorPriceWhereInput | null
}
要仅获得至少有一个 CollectionFloorPrice
的 Collection
,您应该使用 some
(而不是 exists
)并指定一个始终 return 对任何存在的相关记录为真。
如果您希望您的查询包含相关的 CollectionFloorPrice
,您必须在 include
属性.
中指定它
return await this.prisma.collection.findMany({
where: {
floorPrices: {
some: {
id: { not: "" } // It always should be true.
},
},
},
// if you want to include related floor prices in returned object:
include: {
floorPrices: true,
},
});
我有 2 个表:
model Collection {
id String @id @default(uuid()) @db.Uuid/
floorPrices CollectionFloorPrice[]
}
model CollectionFloorPrice {
id String @id @default(uuid()) @db.Uuid
collection Collection @relation(fields: [collectionId], references: [id])
collectionId String @db.Uuid
}
如何查询只有 CollectionFloorPrice
中存在行的集合?在 SQL 中,这将是一个简单的 JOIN。
这不起作用:
return await this.prisma.collection.findMany({
where: {
floorPrices: {
exists: true,
},
},
});
名为 CollectionFloorPrice
的模型的 Prisma 关系过滤器是:
export type CollectionFloorPriceFilter = {
every?: CollectionFloorPriceWhereInput | null
some?: CollectionFloorPriceWhereInput | null
none?: CollectionFloorPriceWhereInput | null
}
要仅获得至少有一个 CollectionFloorPrice
的 Collection
,您应该使用 some
(而不是 exists
)并指定一个始终 return 对任何存在的相关记录为真。
如果您希望您的查询包含相关的 CollectionFloorPrice
,您必须在 include
属性.
return await this.prisma.collection.findMany({
where: {
floorPrices: {
some: {
id: { not: "" } // It always should be true.
},
},
},
// if you want to include related floor prices in returned object:
include: {
floorPrices: true,
},
});