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: order by m2m with disconnected records #2150

Merged
merged 1 commit into from Aug 17, 2021

Conversation

Weakky
Copy link
Member

@Weakky Weakky commented Aug 12, 2021

Overview

Fixes prisma/prisma#8036

Count aggregations on m2m were done from the join table and joined the relation tables. While this worked, it only returned records that had a count > 0.

SELECT A.id, COUNT(*) AS <AGGREGATOR_ALIAS> FROM _AtoB
  INNER JOIN A ON A.id = _AtoB.A
  INNER JOIN B ON B.id = _AtoB.B
GROUP BY A.id

This flawed the cursor logic, which needs to have all the records (the ones with a relation count > 0 and the ones without any connected records) to properly truncate the result set.

Instead of selecting from the join table (which contains only the connected records), we're now selecting from the base model and left joining the join table.

SELECT A.id, COUNT(_AtoB.B) AS <AGGREGATOR_ALIAS> FROM A
  LEFT JOIN _AtoB ON (A.id = _AtoB.B)
GROUP BY A.id

@Weakky Weakky added this to the 2.30.0 milestone Aug 13, 2021
@Weakky Weakky force-pushed the fix/order-by-rel-m2m-cursor branch from 1ac31c0 to 5be1604 Compare August 13, 2021 08:55
@Weakky Weakky merged commit efdfe2b into master Aug 17, 2021
@Weakky Weakky deleted the fix/order-by-rel-m2m-cursor branch August 17, 2021 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Order By Relation with CursorId doesn't return result
2 participants