question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shows incorrect count number by using selectRelationCount previewFeatures

See original GitHub issue

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);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
oceandramacommented, Aug 6, 2021

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
        }
      }
    ]
  }
]
0reactions
janpiocommented, Sep 5, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

count self relation on Prisma error: table name specified more ...
This is a known issue with self-relations and we hope to fix it soon. If you want to track this bug, follow this...
Read more >
Prisma 2.30.0 Release - GitClear
These scenarios tend to show up often for developers using the ... Shows incorrect count number by using selectRelationCount previewFeatures · JSON number ......
Read more >
What's new in Prisma? (Q3/2021)
Select Relation Count allows you to count the number of related records by passing _count to the select or include options and then...
Read more >
prisma argument of type whereuniqueinput needs at least one ...
I'm trying to implement resolver which will check if the user with given login exists in the database. But I have a hard...
Read more >
Prisma-whats-new - Linen
generator client { provider = "prisma-client-js" previewFeatures ... You can find out more about implementing use cases with transactions in the docs, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found