首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Prisma js ORM --如何过滤相关表中有条目的结果(有效地加入)?

Prisma js ORM --如何过滤相关表中有条目的结果(有效地加入)?
EN

Stack Overflow用户
提问于 2022-03-16 15:57:28
回答 2查看 1.8K关注 0票数 0

我有两张桌子:

代码语言:javascript
复制
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中,这将是一个简单的联接。

这不管用:

代码语言:javascript
复制
    return await this.prisma.collection.findMany({
      where: {
        floorPrices: {
          exists: true,
        },
      },
    });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-17 04:54:01

Prisma对一个名为CollectionFloorPrice的模型的关系过滤器如下:

代码语言:javascript
复制
export type CollectionFloorPriceFilter = {
  every?: CollectionFloorPriceWhereInput | null
  some?: CollectionFloorPriceWhereInput | null
  none?: CollectionFloorPriceWhereInput | null
}

要只获得至少有一个CollectionCollectionFloorPrice,您应该使用some (而不是exists),并指定一个条件,该条件总是对存在的任何相关记录返回true。

如果希望查询包括相关的CollectionFloorPrice,则必须在include属性中指定它。

代码语言:javascript
复制
 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,
   },
 });
票数 4
EN

Stack Overflow用户

发布于 2022-10-19 09:08:13

代码语言:javascript
复制
      prisma.collection.findMany({
        where: { floorPrices: { some: {} } }
      })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71500284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档