Skip to content

Preview feature feedback: Find a relation's count (selectRelationCount ) #6312

Closed

Description

@matthewmueller

Please share your feedback about the selectRelationCount functionality released in v2.20.0 in this issue.

  • If you encounter a bug, please open a bug report in this repo.
  • If the feature is working well for you, please share this in a comment below or leave a 👍 on this issue.

If you have any questions, don't hesitate to ask them in the #prisma-client channel in the Prisma Slack.

Activity

  1. janpio commented on May 22, 2021

    @janpio
    Contributor

    There is one bug at #6543 that was reported multiple times now.

  2. 41y08h commented on May 22, 2021

    @41y08h

    There is one bug at #6543 that was reported multiple times now.

    Nested relation count is the feature that is not working and produces this error.
    Hoping to see prisma resolve it fast.

    Thanks.

  3. codasana commented on Jun 2, 2021

    @codasana

    Hi,

    I have this query:

    const user = await prisma.user.findFirst({
                where: { username: username },
                include: {
                    posts: {
                        include: {
                            likedBy:true,
                        } 
                    },
                }
            })
    

    I’m getting a user with a specific user name, and I’m also getting the posts for the user. And for each post, I’m getting likedBy (which is a relation).

    This works perfectly.

    However, instead of getting the array of likedBy, if I want to get the count of liked by, I do the following:

    const user = await prisma.user.findFirst({
                where: { username: username },
                include: {
                    posts: {
                        include: {
                            _count: {
                                select: {
                                    likedBy:true
                                }
                            }
                        } 
                    },
                }
            })
    

    This query fails with error: PANIC in query-engine.

    Looks like: Currently the _count parameter can only be used on a top level include or select and not nested levels.

    Can this be supported, please.

  4. SamuelMS commented on Jun 15, 2021

    @SamuelMS

    Opened #7688 to track this issue, but the _count field doesn't seem to be supported by Prisma.validator and Prisma.XYZGetPayload for partial / related types.

    // works fine
    const user = Prisma.validator<Prisma.UserArgs>()({
      select: { id: true, name: true },
    });
    export type User = Prisma.UserGetPayload<typeof user>;
    
    // TS2322: Type 'boolean' is not assignable to type 'never'.
    const userWithCount = Prisma.validator<Prisma.UserArgs>()({
      select: { id: true, name: true, _count: { select: { posts: true } },  },
    });
  5. zfanta commented on Aug 13, 2021

    @zfanta

    Hi,

    I hope the fields of SomeCountOutputTypeSelect support not only boolean type.

    I have this schema.

    model Post {
      id      String   @id @db.VarChar(32)
      body    String   @db.Text
      reports Report[]
    }
    
    model Report {
      id       String  @id @db.VarChar(32)
      postId   String? @db.VarChar(32)
      post     Post?   @relation(fields: [postId], references: [id], onDelete: Cascade)
      reason   String? @db.Text
      validity Boolean @default(true)
    
      @@index([postId])
    }
    

    And I have to count the valid reports of post. So I use this code that manually calls count function for every post.

    const post = await prisma.post.findUnique({
      where: { id: 'some id' },
      include: {
        _count: {
          select: {
            reports: true
          }
        }
      }
    })
                                                     
    post._count.reports = await prisma.report.count({
      where: {
        postId: 'some id',
        validity: true
      }
    })
    

    If the fields of SomeCountOutputTypeSelect are boolean|SomeWhereInput, I can use this code.

    const post = await prisma.post.findMany({
      where: { id: 'some id' },
      include: {
        _count: {
          select: {
            reports: {
              validity: true
            }
          }
        }
      }
    })
    
  6. added this to the 2.31.0 / 3.0.x milestone on Aug 19, 2021
  7. andrelandgraf commented on Aug 28, 2021

    @andrelandgraf

    Is this preview feature also supported with mongoDB?

  8. 2color commented on Sep 7, 2021

    @2color
    Contributor

    @andrelandgraf You cannot currently use selectRelationCount with the MongoDB connector.

  9. 2color commented on Sep 7, 2021

    @2color
    Contributor

    Closing this now that this has been released to General Availability in the 3.0.1 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: previewFeaturestopic: selectRelationCount

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions