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

fix: relation count on self m2m #2144

Merged
merged 3 commits into from Aug 12, 2021
Merged

fix: relation count on self m2m #2144

merged 3 commits into from Aug 12, 2021

Conversation

Weakky
Copy link
Member

@Weakky Weakky commented Aug 11, 2021

Overview

Fixes prisma/prisma#7807
Fixes prisma/prisma#7893

As we perform inner joins on both sides in the case of an m2m, this PR adds aliases to the joins so that the same table is not referred to twice in the same block.

Example

Given this datamodel:

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

And the following query:

query {
  findManyUser {
    id
    _count {
      following
    }
  }
}

Here's what the LEFT JOIN in charge of computing the count aggregation would look like before:

LEFT JOIN (
  SELECT
    "public"."User"."id",
    COUNT(*) AS "_aggr_count_following"
  FROM
    "public"."_approved-followers"
      INNER JOIN "public"."User" ON ("public"."User"."id" = ("public"."_approved-followers"."B"))
      INNER JOIN "public"."User" ON ("public"."User"."id" = ("public"."_approved-followers"."A"))
	GROUP BY "public"."User"."id"
) AS "aggr_selection_0_User" ON ("public"."User"."id" = "aggr_selection_0_User"."id")

And after (notice the aliases on the INNER JOINs):

LEFT JOIN (
  SELECT
    User_A."id",
    COUNT(*) AS "_aggr_count_following"
  FROM
    "public"."_approved-followers"
      INNER JOIN "public"."User" AS User_A ON (User_A."id" = ("public"."_approved-followers"."B"))
      INNER JOIN "public"."User" AS User_B ON (User_B."id" = ("public"."_approved-followers"."A"))
	GROUP BY User_A."id"
) AS "aggr_selection_0_User" ON ("public"."User"."id" = "aggr_selection_0_User"."id")

@Weakky Weakky added this to the 2.30.0 milestone Aug 11, 2021
@Weakky Weakky merged commit ec4e7ae into master Aug 12, 2021
@Weakky Weakky deleted the fix/rel-count-m2m-self branch August 12, 2021 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants