Skip to content

Shows incorrect count number by using selectRelationCount previewFeatures #8050

Closed
prisma/prisma-engines
#2143

Description

@ikramhasib007

I have a list of posts and each post has multiple comments. I was counting the total comments that each post has. Using selectRelationCount previewFeatures, its count number is correct but it shows different references. like:
post id 1 has 2 comments
post id 2 has 4 comments
post id 3 has 0 comment
post id 4 has 0 comment

query result shows:
post id 1 has 0 comments
post id 2 has 0 comments
post id 3 has 4 comment
post id 4 has 2 comment

Query:

 ..
 select: {
      ...select.select.posts.select,
      _count: {
        select: { comments: true }
      }
    }
 .
 .
 const posts = await prisma.post.findMany(opArgs);

Activity

  1. added
    bug/0-unknown
    kind/bug
    on Jul 4, 2021
  2. matthewmueller commented on Jul 6, 2021

    @matthewmueller
    Contributor

    Hey @ikramhasib007, can you share an example schema and script so we can reproduce?

  3. added
    topic: previewFeatures
    on Jul 6, 2021
  4. ikramhasib007 commented on Jul 6, 2021

    @ikramhasib007
    Author

    schema.prisma File

    model User {
      id        String  @id       @default(cuid())
      name      String
      email     String  @unique
      password  String?
      posts     Post[]
      comments  Comment[]
      createdAt DateTime          @default(now())
      updatedAt DateTime          @updatedAt
    
      @@index([name, email])
    }
    
    model Post {
      id        String  @id       @default(cuid())
      title     String
      body      String
      published Boolean
      author    User              @relation(fields: [authorId], references: [id])
      authorId  String
      comments  Comment[]
      createdAt DateTime          @default(now())
      updatedAt DateTime          @updatedAt
    
      @@index([title])
    }
    
    model Comment {
      id        String  @id       @default(cuid())
      text      String
      post      Post              @relation(fields: [postId], references: [id])
      postId    String       
      author    User              @relation(fields: [authorId], references: [id])
      authorId  String
      createdAt DateTime          @default(now())
      updatedAt DateTime          @updatedAt
    }
    

    schema.graphql File

    type Query {
      posts(query: String, skip: Int, take: Int, cursor: String): [Post!]!
      feed(query: String, take: Int, cursor: String): Feed
      comments(skip: Int, take: Int, cursor: String): [Comment!]!
    }
    
    type User {
      id: ID!
      name: String!
      email: String
      password: String
      posts(
        query: String
        cursor: String
        take: Int
        skip: Int
      ): [Post!]!
      comments(
        cursor: String
        take: Int
        skip: Int
      ): [Comment!]!
      createdAt: String!
      updatedAt: String!
    }
    
    type Post {
      id: ID!
      title: String!
      body: String!
      published: Boolean!
      author: User!
      comments(
        cursor: String
        take: Int
        skip: Int
      ): [Comment!]!
      createdAt: String!
      updatedAt: String!
      _count: Count
    }
    
    type Comment {
      id: ID!
      text: String!
      author: User!
      post: Post!
      createdAt: String!
      updatedAt: String!
    }
    
    type Count {
      comments: Int
    }
    
    type Feed {
      hasNextPage: Boolean
      posts(
        query: String
        cursor: String
        take: Int
        skip: Int
      ): [Post!]!
    }
    

    Queries resolvers

    import { PrismaSelect } from '@paljs/plugins'
    .
    .
    
    async feed(parent, args, { prisma }, info) {
        const select = new PrismaSelect(info).value
        try {
          const opArgs = {
            take: args.take ? args.take + 1 : 5 + 1,
            where: {
              published: true
            },
            orderBy: {
              createdAt: 'desc'
            },
            select: {
              ...select.select.posts.select
            }
          }
          if(typeof args.cursor === 'string') {
            opArgs.cursor = {
              id: args.cursor
            }
          }
          if(args.query) {
            opArgs.where.OR = [{
              title: {
                contains: args.query
              }
            }, {
              body: {
                contains: args.query
              }
            }]
          }
          if(opArgs.cursor) {
            opArgs.skip = 1
          }
          const posts = await prisma.post.findMany(opArgs);
          const feed = {
            hasNextPage: posts.length === opArgs.take,
            posts: posts.length === opArgs.take ? posts.slice(0, -1) : posts
          }
          return feed;
        } catch (error) {
          throw error
        }
      },
    

    package.json

    "dependencies": {
        "@apollo/client": "^3.3.15",
        "@babel/polyfill": "^7.12.1",
        "@graphql-tools/graphql-file-loader": "^6.2.7",
        "@graphql-tools/load": "^6.2.8",
        "@graphql-tools/schema": "^7.1.5",
        "@paljs/plugins": "^3.6.0",
        "@prisma/client": "^2.26.0",
        "bcryptjs": "^2.4.3",
        "cross-fetch": "^3.0.6",
        "cuid": "^2.1.8",
        "env-cmd": "^10.1.0",
        "express": "^4.17.1",
        "faker": "^5.4.0",
        "graphql": "^15.5.0",
        "graphql-postgres-subscriptions": "^1.0.5",
        "graphql-subscriptions": "^1.2.1",
        "graphql-yoga": "^1.18.3",
        "http-errors": "^1.8.0",
        "jsonwebtoken": "^8.5.1",
        "mkdirp": "^1.0.4",
        "nanoid": "^3.1.23",
        "pg": "^8.6.0",
        "subscriptions-transport-ws": "^0.9.19"
      },
      "devDependencies": {
        "@babel/cli": "^7.12.16",
        "@babel/core": "^7.12.16",
        "@babel/node": "^7.12.16",
        "@babel/plugin-proposal-object-rest-spread": "^7.12.13",
        "@babel/preset-env": "^7.12.16",
        "@babel/register": "^7.13.8",
        "jest": "^26.6.3",
        "nodemon": "^2.0.7",
        "prisma": "^2.26.0"
      }
    

    @matthewmueller , check out the source code.
    Note: normal queries are ok for relational counts. But when use cursor & skip for pagination then that incorrect count number happens.

  5. added
    bug/1-unconfirmed
    and removed
    bug/0-unknown
    on Jul 16, 2021
  6. oceandrama commented on Aug 6, 2021

    @oceandrama

    Have the similar problem here, even without using skip or cursor

    Schema:

    datasource db {
      provider = "postgres"
      url      = env("DATABASE_URL")
    }
    
    generator client {
      provider        = "prisma-client-js"
      previewFeatures = ["nApi", "referentialActions", "selectRelationCount"]
    }
    
    enum UserRole {
      USER
      ADMIN
    }
    
    model User {
      id        Int      @id @default(autoincrement())
      createdAt DateTime @default(now())
      updatedAt DateTime @updatedAt
    
      name           String
      email          String   @unique
      hashedPassword String?
      role           UserRole @default(USER)
    
      authoredEvents Event[]
      participations Participant[]
    }
    
    model City {
      id        Int      @id @default(autoincrement())
      createdAt DateTime @default(now())
      updatedAt DateTime @updatedAt
    
      name String
    
      locations Location[]
    }
    
    model Location {
      id        Int      @id @default(autoincrement())
      createdAt DateTime @default(now())
      updatedAt DateTime @updatedAt
    
      name        String
      description String?
      coordinates Int[]
    
      city   City    @relation(fields: [cityId], references: [id])
      cityId Int
      events Event[]
    }
    
    enum GroupRole {
      PACER
      PARTICIPANT
    }
    
    enum ParticipationStatus {
      REGISTERED
      STARTED
      FINISHED
    }
    
    model Participant {
      createdAt DateTime @default(now())
    
      role   GroupRole
      status ParticipationStatus @default(REGISTERED)
    
      group   Group @relation(fields: [groupId], references: [id], onDelete: Cascade)
      groupId Int
      user    User  @relation(fields: [userId], references: [id], onDelete: Cascade)
      userId  Int
    
      @@id([groupId, userId])
    }
    
    model Group {
      id        Int      @id @default(autoincrement())
      createdAt DateTime @default(now())
      updatedAt DateTime @updatedAt
    
      pace      String
      startTime DateTime @db.Time
      limit     Int?
    
      event        Event         @relation(fields: [eventId], references: [id], onDelete: Cascade)
      eventId      Int
      participants Participant[]
    }
    
    model Event {
      id        Int      @id @default(autoincrement())
      createdAt DateTime @default(now())
      updatedAt DateTime @updatedAt
    
      distance    Int
      description String?
      startDate   DateTime @db.Date
    
      location   Location @relation(fields: [locationId], references: [id])
      locationId Int
      author     User?    @relation(fields: [authorId], references: [id])
      authorId   Int?
      groups     Group[]
    }
    
    

    Query:

    db.event.findMany({
      select: {
        id: true,
        groups: {
          select: {
            id: true,
            participants: true,
            _count: {
              select: {
                participants: true,
              },
            },
          },
        },
      },
    })
    

    Result (attention to groups with id 12 and 14):

    [
      {
        "id": 5,
        "groups": [
          {
            "id": 11,
            "participants": [
              {
                "createdAt": "2021-08-06T12:42:16.807Z",
                "role": "PARTICIPANT",
                "status": "REGISTERED",
                "groupId": 11,
                "userId": 1
              }
            ],
            "_count": {
              "participants": 1
            }
          },
          {
            "id": 12,
            "participants": [],
            "_count": {
              "participants": 1
            }
          },
          {
            "id": 10,
            "participants": [],
            "_count": {
              "participants": 0
            }
          }
        ]
      },
      {
        "id": 6,
        "groups": [
          {
            "id": 14,
            "participants": [
              {
                "createdAt": "2021-08-05T21:52:26.548Z",
                "role": "PARTICIPANT",
                "status": "REGISTERED",
                "groupId": 14,
                "userId": 1
              }
            ],
            "_count": {
              "participants": 0
            }
          },
          {
            "id": 13,
            "participants": [],
            "_count": {
              "participants": 0
            }
          }
        ]
      }
    ]
    
  7. pantharshit00 commented on Aug 9, 2021

    @pantharshit00
    Contributor

    Thanks for the details, I can reproduce this now.

    Posted minimal reproduction here: https://github.com/harshit-test-org/prisma-issue-8050

  8. removed their assignment
    on Aug 9, 2021
  9. added
    bug/2-confirmed
    and removed
    bug/1-unconfirmed
    on Aug 9, 2021
  10. added this to the 2.30.0 milestone on Aug 11, 2021
  11. mart2967 commented on Sep 5, 2021

    @mart2967

    I'm seeing this happen on 2.30.3 - have tried removing where, skip / take, orderBy, without success. Counts and length of selected relations don't match.

    db.entity.findMany({
           where: { relatedEntity: { otherRelatedEntity: { id } } }
           skip,
            take,
            orderBy: { createdAt: 'desc' },
            select: {
              id: true,
              singleRelation: {
                select: {
                  // returns array with correct length
                  ManyRelation: {
                    select: {
                      id: true
                    }
                  },
                  // This returns correct counts attributed to the wrong rows
                  _count: {
                     select: {
                       ManyRelation: true
                     }
                   }
                }
              }
            }
          })
    
  12. janpio commented on Sep 5, 2021

    @janpio
    Contributor

    Can you open a separate issue please @mart2967 and provide all the information the issue template asks for? Thanks.

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

Metadata

Metadata

Assignees

Labels

bug/2-confirmedkind/bugtopic: previewFeaturestopic: selectRelationCount

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions