Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prisma client crashes when counting m-n self-relation #7807

Closed
Tracked by #8628
nichitaS40 opened this issue Jun 22, 2021 · 3 comments · Fixed by prisma/prisma-engines#2144
Closed
Tracked by #8628

Prisma client crashes when counting m-n self-relation #7807

nichitaS40 opened this issue Jun 22, 2021 · 3 comments · Fixed by prisma/prisma-engines#2144
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: selectRelationCount
Milestone

Comments

@nichitaS40
Copy link

nichitaS40 commented Jun 22, 2021

Bug description

Running a basic findUnique which includes

 _count: {
        select: {
          posts: true,
          followers: true
        }
}

Crashes prisma with this error:

Screenshot 2021-06-22 at 17 47 47

The user model contains this:

  followers User[] @relation(name: "approved-followers", references: [id])
  following User[] @relation(name: "approved-followers", references: [id])

If i remove the followers: true variable, and leave only the posts count, that works correctly.

How to reproduce

Create a model, add an m-n self relation to it, then run a query which includes the count of one of those relations

Expected behavior

It should either return the count, or throw a nice error message. At the moment it crashes

Prisma information

model User {
  id              Int       @id @default(autoincrement())
  ...
  followers User[] @relation(name: "approved-followers", references: [id])
  following User[] @relation(name: "approved-followers", references: [id])
   ...
}

Screenshot 2021-06-22 at 17 51 27

Environment & setup

  • OS:
  • Database:
  • Node.js version:

MacOS, PostgreSQL database

Node is: v16.3.0

Prisma Version

prisma               : 2.23.0
@prisma/client       : 2.23.0
Current platform     : darwin
Query Engine         : query-engine adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : adf5e8cba3daf12d456d911d72b6e9418681b28b
Studio               : 0.393.0
Preview Features     : selectRelationCount
@nichitaS40 nichitaS40 added the kind/bug A reported bug. label Jun 22, 2021
@nichitaS40
Copy link
Author

After some more debugging, this is the query that prisma generates:

SELECT "public"."User"."id", "public"."User"."email", "public"."User"."username", "public"."User"."password", "public"."User"."firstName", "public"."User"."lastName", "public"."User"."age", "public"."User"."emailVerified", "public"."User"."approved", "public"."User"."profilePicture", "public"."User"."description", "public"."User"."refreshToken", "aggr_selection_0_User"."_aggr_count_followers" FROM "public"."User" LEFT JOIN (SELECT "public"."User"."id", COUNT(*) AS "_aggr_count_followers" FROM "public"."_approved-followers" INNER JOIN "public"."User" ON ("public"."User"."id" = ("public"."_approved-followers"."A")) INNER JOIN "public"."User" ON ("public"."User"."id" = ("public"."_approved-followers"."B")) GROUP BY "public"."User"."id") AS "aggr_selection_0_User" ON ("public"."User"."id" = "aggr_selection_0_User"."id") WHERE "public"."User"."id" = $1 LIMIT $2 OFFSET $3

Looks like the problem is the two inner joins, only one of those really makes sense (though i'm not sure which)

@nichitaS40
Copy link
Author

nichitaS40 commented Jun 22, 2021

After even more debugging, it looks like the issue is in this line:

https://github.com/prisma/prisma-engines/blob/master/query-engine/connectors/sql-query-connector/src/join_utils.rs#L123

Here I am stuck however :( I don't understand why it would do 2 inner joins (since it would seem like the first one is enough to count relations). But you guys are the experts, hopefully this shortens the tracking down time a bit!

Edit: And indeed, running the query with the second inner join removed (for a different m-n query) yields the same result as the prisma version.. perhaps there's a special case I'm not considering where the second one is required?

@janpio janpio added the team/client Issue for team Client. label Jun 30, 2021
@pantharshit00
Copy link
Contributor

I can confirm this bug. We will have a proper look here.

I posted a minimal reproduction for this here: https://github.com/harshit-test-org/prisma-issue-7807

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: selectRelationCount
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants