prisma icon indicating copy to clipboard operation
prisma copied to clipboard

Error when mixing regular field and relation field in `orderBy`

Open ymc9 opened this issue 11 months ago • 6 comments

Bug description

The Prisma find API allows the use of relation fields in the orderBy clause. However, it rejects the query when a regular field and a relation field are used together.

How to reproduce

Use the schema and TS code in the "Prisma information" section. Running the code results in the following error:

Argument `orderBy`: Invalid value provided. Expected PostOrderByWithRelationInput[], provided Object.
    at Cn (/private/tmp/foo/node_modules/@prisma/client/runtime/library.js:116:5888)
    at _n.handleRequestError (/private/tmp/foo/node_modules/@prisma/client/runtime/library.js:123:6510)
    at _n.handleAndLogRequestError (/private/tmp/foo/node_modules/@prisma/client/runtime/library.js:123:6188)
    at _n.request (/private/tmp/foo/node_modules/@prisma/client/runtime/library.js:123:5896)
    at async l (/private/tmp/foo/node_modules/@prisma/client/runtime/library.js:128:10871) {
  clientVersion: '5.10.2'
}

Expected behavior

Returns correct result.

Prisma information

Schema

datasource db {
    provider = "sqlite"
    url = "file:./dev.db"
}

generator client {
    provider = "prisma-client-js"
}

model User {
    id Int @id() @default(autoincrement())
    email String @unique()
    name String?
    posts Post[]
}

model Post {
    id Int @id() @default(autoincrement())
    title String
    content String?
    published Boolean @default(false)
    author User? @relation(fields: [authorId], references: [id])
    authorId Int?
}

Code

prisma.user.findUnique({
  where: { id: user1.id },
  include: {
      posts: {
          orderBy: {
              title: 'asc',
              author: {
                  name: 'asc',
              },
          },
      },
  },
});

Environment & setup

  • OS: macOS
  • Database: SQLite
  • Node.js version: v20.10.0

Prisma Version

prisma                  : 5.10.2
@prisma/client          : 5.10.2
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v20.10.0
Query Engine (Node-API) : libquery-engine 5a9203d0590c951969e85a7d07215503f4672eb9 (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli 5a9203d0590c951969e85a7d07215503f4672eb9 (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.10.0-34.5a9203d0590c951969e85a7d07215503f4672eb9
Default Engines Hash    : 5a9203d0590c951969e85a7d07215503f4672eb9
Studio                  : 0.499.0

ymc9 avatar Mar 06 '24 20:03 ymc9

Note: could be related to https://github.com/prisma/prisma/issues/21469 🤔 Maybe this is related to author being an optional relation on your Post model.

@ymc9 We would need to reproduce this, if you switch the author to be required, let us know if that changes anything.

Jolg42 avatar Mar 07 '24 08:03 Jolg42

Note: could be related to #21469 🤔 Maybe this is related to author being an optional relation on your Post model.

@ymc9 We would need to reproduce this, if you switch the author to be required, let us know if that changes anything.

Hi @Jolg42 , thanks for following up. I can confirm the same error happens after changing author to required.

ymc9 avatar Mar 10 '24 01:03 ymc9

This seems to happen here to, without relations. hostname is just a string I get the error

"Argument `orderBy`: Invalid value provided. Expected ServerOrderByWithRelationInput[], provided Object.
const queryBody: Prisma.ServerFindManyArgs = {
    skip: params.skip,
    take: params.take,
    orderBy: {
        id: 'desc',
        hostname: 'asc',
    },
    where: {
        ...where,
        company: {
            name: companyName,
        },
    },
};

azertyalex avatar Mar 21 '24 15:03 azertyalex

I'm experiencing the same issue.

db.user.findMany({
  orderBy: { 
    createdAt: 'asc', 
    updatedAt: 'asc' 
  }
})

I get the following error:


Invalid `prisma.user.findMany()` invocation:

{
   orderBy: {
    createdAt: \"asc\",
    updatedAt: \"asc\"
  }
  ~~~~~~~~~~~~~~~~~~
}

Argument `orderBy`: Invalid value provided. Expected UserOrderByWithRelationInput[], provided Object."

rolandlgnd avatar Apr 18 '24 11:04 rolandlgnd

I had exactly the same issue and this is what I ended up using according to orderBy?: OrderOrderByWithRelationInput | OrderOrderByWithRelationInput[]. Not sure if those two are the same though.

db.user.findMany({
  orderBy: [
    {createdAt: 'asc'},
    {updatedAt: 'asc'},
  ]
})

ibio avatar Apr 22 '24 06:04 ibio

Just experienced this same error and I can confirm is not related to sorting relations because I'm not sorting by relation but by two strings values, one of which is nullable.

focux avatar Apr 26 '24 14:04 focux

Hi @ymc9, I can reproduce this bug Prisma 5.15.0, obtaining the following output:

Invalid `prisma.user.findUnique()` invocation in
/Users/jkomyno/work/prisma/prisma/sandbox/basic-sqlite/index.ts:11:34

   8 
   9 await createUsersWithPosts(prisma)
  10 
→ 11 const user = await prisma.user.findUnique({
       where: {
         id: 1
       },
       include: {
         posts: {
           orderBy: {
             title: "asc",
             author: {
               name: "asc"
             }
           }
           ~~~~~~~~~~~~~~~~~~~~~~~
         }
       }
     })

Argument `orderBy`: Invalid value provided. Expected PostOrderByWithRelationInput[], provided Object.

It also seems that this should be caught by a type error as well, which is currently not the case.

jkomyno avatar Jun 11 '24 12:06 jkomyno