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

nativeDistinct doesn't work when we include orderBy or take and limit in the queries #22734

Open
meotimdihia opened this issue Jan 21, 2024 · 1 comment
Assignees
Labels
domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/improvement An improvement to existing feature and code. tech/engines/query engine Issue in the Query Engine topic: limit topic: nativeDistinct topic: orderBy / sorting topic: postgresql topic: take

Comments

@meotimdihia
Copy link

meotimdihia commented Jan 21, 2024

Bug description

Native DISTINCT doesn't work when we used orderBy

          await fastify.prisma.user_info.findMany({
            distinct: ["role"],

            select: {
              role: true
            },
            orderBy: {
              role: {
                sort: "desc",
                nulls: "last"
              }
            }
          })

or use take and limit

          await fastify.prisma.user_info.findMany({
            distinct: ["role"],
            take: req.query.limit,
            skip: req.query.page * req.query.limit,
            select: {
              role: true
            }
          })

The raw queries when used these queries:

SELECT "public"."user_info"."id", "public"."user_info"."role"::text FROM "public"."user_info" WHERE 1=1 ORDER BY "public"."user_info"."id" ASC OFFSET 0

It looks like we have to work around this problem by using subqueries: https://stackoverflow.com/questions/9795660/postgresql-distinct-on-with-different-order-by

How to reproduce

.

Expected behavior

No response

Prisma information

I posted them.

Environment & setup

  • OS: macOS
  • Database: PostgreSQL
  • Node.js version: 20

Prisma Version

    "prisma": "5.8.1",
@meotimdihia meotimdihia added the kind/bug A reported bug. label Jan 21, 2024
@Druue
Copy link
Contributor

Druue commented Jan 22, 2024

Hey, @meotimdihia, this is work that is still in progress and as such, this is expected behaviour currently.

You can find the PR in our engines here #4525 if you would like to track progress

@Druue Druue added tech/engines/query engine Issue in the Query Engine domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. topic: nativeDistinct labels Jan 22, 2024
@Druue Druue self-assigned this Jan 22, 2024
@aqrln aqrln added kind/improvement An improvement to existing feature and code. and removed kind/bug A reported bug. labels Jan 29, 2024
@janpio janpio changed the title Native DISTINCT doesn't work when we include orderBy or take and limit in the queries nativeDistinct doesn't work when we include orderBy or take and limit in the queries Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/improvement An improvement to existing feature and code. tech/engines/query engine Issue in the Query Engine topic: limit topic: nativeDistinct topic: orderBy / sorting topic: postgresql topic: take
Projects
None yet
Development

No branches or pull requests

5 participants
@janpio @meotimdihia @aqrln @Druue and others