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
Support for Distinct Query on Nested Field #24052
Comments
What is the API you think Prisma Client should offer for this? |
In my opinion, it could either be allowing subfields in the current API (i.e. distinct: {
game: {
categoryId: true
}
} I am unsure of the technical challenges present here, but the raw SQL query (in Postgres) for this operation in particular would look something like SELECT DISTINCT ON ("Game"."categoryId") *
FROM "Play"
INNER JOIN "Game" ON "Play"."gameId" = "Game".id
INNER JOIN "User" ON "Play"."playerId" = "User".id
ORDER BY "Game"."categoryId" ASC, score DESC; |
Problem
Suppose I have the sample schema from the documentation, and I add a
GameCategory
model (with a one-to-many relation toGame
) such that the schema looks like this:In the sample query given with this schema, Prisma queries each player's highest score per game. Suppose I want to be able to query for each player's highest score per game category. That is, I want to be able to use
distinct
oncategoryId
, which I currently can't do sincecategoryId
doesn't exist on the modelPlay
. Is it possible to support using distinct on relation models?Of course, I can filter this myself (which is not ideal with large amounts of data), but given that there is currently a database level
DISTINCT
in preview, I was wondering if it was possible to achieve this query at the database level as well.The text was updated successfully, but these errors were encountered: