digitallyinduced/ihp

"Could not deduce" Error When Sorting Many-To-One Collection Queries

Closed this issue · 4 comments

I wanted to sort the results of a many-to-one relationship query. However, following the example for one-to-many relationships and modifying it for many-to-one, results in a "Could not deduce" error.

Here's an example code:

query @Comment |> fetch
  >>= pure . map (modify #postId (orderByAsc #createdAt))
  >>= collectionFetchRelated #postId

This results in this error:

Could not deduce (HasQueryBuilder Id' joinRegister0)
arising from a use of 'orderByAsc' from the context: (?context :: ControllerContext, ?modelContext :: ModelContext, ?theAction :: CommentsController)
bound by the type signature for ...

The code you've shared is basically saying "fetch all comments, with all their related posts ordered by created at". The problem is that each comment only belongs to a single post, so you cannot order the related posts by created at.

Ah, that makes much more sense than "Could not deduce...".

But how would I sort the results by a relation's field in a many-to-one relationship? As in I want to display a list of all comments along with their corresponding post title and post created timestamp, and sort the result by post created timestamp?

This query cannot be done with the IHP query builder directly. You could either write your own sql query using the sqlQuery function or reorder the results manually using a haskell sort function

Ah, thanks for confirming my suspicions. I think I will use a sortable JavaScript-based table instead.