简体   繁体   English

Prisma 如何使用“where”从关系中获取数据?

[英]Prisma how to get data from relation as using 'where'?

my User prisma schema User model has like[] :我的用户棱镜架构用户模型有like[]

model User {
  likes Like[]
}

and Like model has createdAt .并且 Like 模型已createdAt

model Like {
  id Int @id @default(autoincrement())
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId Int
  createdAt DateTime @default(now())

  @@unique([feedId, userId])
}

I want to get data how many times user links to Like model in this month .我想获取this month user链接到Like模型的次数的数据。 So I get data like below.所以我得到如下数据。

export default {
  Query: {
    seeAllLikeOrder: protectedResolver(() => {
      const startOfMonth = new Date(
        moment().startOf("month").format("YYYY-MM-DD hh:mm").substring(0, 10)
      );
      const endOfMonth = new Date(
        moment().endOf("month").format("YYYY-MM-DD hh:mm").substring(0, 10)
      );
      return client.user.findMany({
        where: {
          likes: {
            createdAt: {
              gte: startOfMonth,
              lt: endOfMonth,
            },
          },
        },
        orderBy: {
          likes: {
            _count: "desc",
          },
        },
        take: 10,
      });
    }),
  },
};

But error comes:但是错误来了:

在此处输入图像描述

I think I can't use according to error message.我想我不能根据错误信息使用。

where: {
          likes: {
            createdAt: {
              gte: startOfMonth,
              lt: endOfMonth,
            },
          },
        },

But i don't understand why.但我不明白为什么。

Because User has Like model and Like has createdAt field.因为 User 有 Like 模型并且 Like 有 createdAt 字段。

in this case, how to get data that I want?在这种情况下,如何获取我想要的数据?

The error is pretty descriptive as to what's going on: createdAt is not a valid property of LikeListRelationFilter , and that type only has the properties every , some , or none .该错误非常描述正在发生的事情: createdAt不是LikeListRelationFilter的有效属性,并且该类型仅具有属性everysomenone

Your issue is the nested select when querying the likes field:您的问题是查询likes字段时的嵌套选择:

return client.user.findMany({
  where: {
    likes: { // This line
      createdAt: {
        gte: startOfMonth,
        lt: endOfMonth,
      },
    },
  },
  orderBy: {
    likes: {
      _count: "desc",
    },
  },
  take: 10,
});

In Prisma, when you are querying a field and filtering based on the value of a nested field, the API for querying that nested array field will be different than if you were querying prisma.like.findMany .在 Prisma 中,当您查询一个字段并根据嵌套字段的值进行过滤时,查询该嵌套数组字段的 API 将与查询prisma.like.findMany不同。 In your situation, your query must look something like this:在您的情况下,您的查询必须如下所示:

return client.user.findMany({
  where: {
    likes: { // This line is different
      // Find any user where at least one of their likes abides by this condition.
      // Replace with "every" to only search for users where ALL of their likes abide by this condition,
      // or "none" to only search for users where NONE of their likes abide by this condition.
      some: { 
        createdAt: {
          gte: startOfMonth,
          lt: endOfMonth,
        },
      }
    },
  },
  orderBy: {
    likes: {
      _count: "desc",
    },
  },
  take: 10,
});

Documentation: https://www.prisma.io/docs/concepts/components/prisma-client/filtering-and-sorting文档: https ://www.prisma.io/docs/concepts/components/prisma-client/filtering-and-sorting

If the nested field was a single item (not an array of items), you would use is or isNot instead.如果嵌套字段是单个项目(不是项目数组),您将使用isisNot代替。 Here you have an array, so you have to use every , some , or none .这里你有一个数组,所以你必须使用everysomenone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM