question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Order by Relations not work

See original GitHub issue

Bug description

Order by Relations not work in version 2.21.2

How to reproduce

My code:

prisma.product.findMany({
    orderBy: {
         analytics: {
             id: 'acs'
         }
      }
})

Type '{ analytics: {}; }' is not assignable to type 'Enumerable<ProductOrderByInput>'.
  Object literal may only specify known properties, and 'analytics' does not exist in type 'Enumerable<ProductOrderByInput>'

Expected behavior

No response

Prisma information

model Product {
  id               Int            @id @default(autoincrement())
  analytics       Analytic[]    @relation("fk_product_analytic")
  @@map("products")
}

model Analytic {
  id                        Int                   @id @default(autoincrement())
  product_id                Int 
  product                   Product               @relation("fk_product_analytic", fields: [product_id], references: [id])

  @@map(name:"product_analytics")
}

Environment & setup

  • Node: 14.16.0

Prisma Version

2.21.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
M15sycommented, May 20, 2022

Hi @NguyenKyThinh94,

Not sure if you’ve already managed to get around your issue (or if this will be helpful to anyone else reading this) but I was also seeing this error in v2.30.3.

In my situation, when I actually ran my app the order by relation was working but I was seeing the above type error. To get rid of the error I just cast my object to the expected type and it seemed to be happy enough with it.

So in your example, something along the lines of:

const orderBy = { analytics: { id: ‘acs’ } } as ProductOrderByInput
prisma.product.findMany({ orderBy })

Thanks

0reactions
arielvieiracommented, Dec 24, 2022

@fabriciosautner I believe you can do it reverse though user_challenges making it distinct by challengeId and ordering it, then selecting the challenge from user_challenges and returning the values in a map

const result = await prisma.user_challenges.findMany({
  // In order to only get one `challenge` per `user_challenges` 
  distinct: 'challengeId',

  // order the `user_challenges.end_date` by desc
  orderBy: {
    end_date: 'desc',
  },

  // includes the `challenge` in the query
  select: {
    challenge: true,
  },

  // your query
  where: {
    user_id,
    completed: true,
    is_current: true,
  },
});

const challengesFound = result.map(r => r.challenge)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel Orderby not working inside Eloquent whereHas ...
Already ASC and DESC tried. Try to order by in whereHas but it's not affect on any records. Below is model relationship. public...
Read more >
Eager Loading orderBy not working as expected - Laracasts
I am trying to run eager loading with constraint by doing multiple with closure. ... The order by is added only to the...
Read more >
Ordering database queries by relationship columns in Laravel
In this article we're going to explore how to order database queries by the value (column) of an Eloquent relationship.
Read more >
Guide to table relationships - Microsoft Support
The relationship between the Customers table and the Orders table is a one-to-many ... This relationship is not common because, most often, the...
Read more >
Eloquent, order by relationship and eager loading
How to order a query results on a one-to-one relationship in Eloquent. Since ´with()´ is creating another query ... So this will not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found